springboot版本为2.2.2.RELEASE,jdk为1.8,swagger为2.6.1
1、修改pom文件,添加swagger依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-staticdocs</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>io.github.robwin</groupId>
<artifactId>markup-document-builder</artifactId>
<version>0.1.5</version>
</dependency>
<dependency>
<groupId>io.github.robwin</groupId>
<artifactId>swagger2markup</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj-pdf</artifactId>
<version>1.5.3</version>
</dependency>
2、测试controller中添加注解
@RestController
@Api(value="hello",description="测试get请求/hello")
public class HelloController {
@RequestMapping("/hello")
@ApiOperation(value="say hello",notes="hello",httpMethod ="GET",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String sayHello(){
return "Hello!";
}
}
3、访问路径:http://127.0.0.1:8081/swagger-ui.html
效果图