1. Maven依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
2. 配置类
@Configuration
@EnableSwagger2
public class Swagger {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("PM Supply")
.description("版本")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}
3. 示例
@Api(value = "用户管理类")
@RestController
@RequestMapping(value = "user")
public class UserController {
@RequestMapping(value = "/user", method = RequestMethod.POST)
@ApiOperation(value = "增加一个用户")
public String addUser(@RequestBody User user) {
return user.getName();
}
}
4. 访问URL
http://localhost:8080/swagger-ui.html
@Api(value = "用户管理类")
@RestController
@RequestMapping(value = "user")
public class UserController {
@RequestMapping(value = "/user", method = RequestMethod.POST)
@ApiOperation(value = "增加一个用户")
public String addUser(@RequestBody User user) {
return user.getName();
}
}