使用Swagger导出rest接口文档
依赖的jar包在pom.xml中。
Swagger的简单配置如下:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
Predicate<RequestHandler> predicate = input -> true;
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.useDefaultResponseMessages(false)
.select()
.apis(predicate)
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("我的docker服务API文档")
在SpringMVC的方法上使用Swagger的注解
@RestController
@RequestMapping("/api")
@Api(value = "user", description = "用户管理", produces = MediaType.APPLICATION_JSON_VALUE)
public class RestApi {
@RequestMapping(value = "/person", method = RequestMethod.GET)
@ApiOperation(value = "获取用户接口", notes = "获取用户接口详细描述")
public Person get(String name, int num) {
return new Person(name, num);
}
@RequestMapping(value = "/person", method = RequestMethod.POST)
@ApiOperation(value = "创建用户接口", notes = "创建用户接口详细描述")
public Map<String, Object> post(Person person) {
System.out.println(person);
HashMap<String, Object> map = Maps.newHashMap();
map.put("status", "ok");
map.put("msg", "are you ok");
return map;
}
}
启动应用,访问 http://localhost:8080/swagger-ui.html#/rest-api,出现Swagger生成的rest接口文档。
