Swagger系列(二) ------- 极致用法

      编写 SpringBoot 项目,项目中 controller 中包含一个 Handler,测试项目,保证程序可以正确运行。

@RestController
@RequestMapping( "/people")

 public  class DemoController {

       @RequestMapping( "/getPeople")
       public People getPeople(Long id, String name){

                 People peo = w new People();
                 peo.setId(id);
                 peo.setName(name);
                 peo.setAddress(" " 聊城" ");
                 return peo;
         }
  }

导入 Spring-fox 依赖
       在项目的pom.xml中导入Spring-fox依赖。目前最新版本为2.9.2,所以导入的依赖也是这个版本。其中 springfox-swagger2 是核心内容的封装。springfox-swagger-ui 是对 swagger-ui 的封装。

<dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.9.2</version>
</dependency>

<dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.2</version>
</dependency>

 添加注解

     在 SpringBoot 的启动类中添加@EnableSwagger2 注解。添加此注解后表示对当前项目中全部控制器进行扫描。应用 Swagger2

@SpringBootApplication
@EnableSwagger2  //关键 

 public  class MyApp {

 public  static  void main(String [] args)

      {
          SpringApplication. run (MyApp. class,args);
      }
}

猜你喜欢

转载自blog.csdn.net/m0_58719994/article/details/121501595
今日推荐