SpringBoot | SpringBoot 微服务整合Swagger生成API文档

关于swagger的优点就不描述了,直接来看一下使用示例:
###1)首先引入swagger依赖:

     
        
            io.springfox
            springfox-swagger2
            2.7.0
        
        
            io.springfox
            springfox-swagger-ui
            2.7.0
        
###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 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);
}

}


###4)swagger api 访问如下:
![这里写图片描述](https://img-blog.csdn.net/20180212182304175?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29zaGlsaWppdXlp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

你可能感兴趣的:(SpringBoot,java)