1, 添加 pom 依赖:
- <!-- 添加 swagger 相关依赖 -->
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>2.7.0</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>2.7.0</version>
- </dependency>
2, 启动类上添加 @EnableSwagger2 注解:
@EnableSwagger2
3, 在 Controller 类上添加 @API 注解, 方法上添加 @ApiOperation 注解:
- package cn.mmweikt.es.controller;
- import io.swagger.annotations.API;
- import io.swagger.annotations.ApiOperation;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.Web.bind.annotation.RequestMapping;
- import org.springframework.Web.bind.annotation.ResponseBody;
- @Controller
- @RequestMapping("test")
- @API("测试")
- public class TestController {
- @GetMapping("test1")
- @ResponseBody
- @ApiOperation("测试")
- public String test(){
- return "test1";
- }
- }
4, 访问 http://127.0.0.1:8080/swagger-ui.html 验证:
来源: http://www.bubuko.com/infodetail-2932973.html