关于swagger的优点就不描述了,直接来看一下使用示例:
1)首先引入swagger依赖:
<!--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)Swagger配置:
/**
* Created by zhangshukang on 2017/11/9.
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.iyb.ak"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("mars组件 RESTful APIs")
.description("mars组件api接口文档")
.version("1.0")
.build();
}
}
3)对应的controller如下:
@RestController
@Slf4j
@RequestMapping(value = "/content")
@Api(value = "ContentsController", description = "内容接口")
public class ContentsController {
@Autowired
ContentsService contentsService;
@ApiOperation(value = "查询内容", notes = "根据id查询内容")
@RequestMapping(value ="/{id}",method = RequestMethod.GET)
public Callable<Contents> getContentById(@PathVariable("id") Integer id) throws Exception {
Contents content = new Contents();
return ()->Arrays.asList(content).get(0);
}
@ApiOperation(value = "修改content", notes = "根据content对象创建角色")
@ApiImplicitParam(name = "修改content", value = "content详细实体", required = true, dataType = "content")
@ApiResponses({ @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对", response = MessageVo.class) })
@RequestMapping(method= RequestMethod.PUT)
public Contents updateContent(@RequestBody Contents contents){
return contentsService.updateByPrimaryKey(contents);
}
}