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

关于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();
}

}


</br>
###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);
}

}


###4)swagger api 访问如下:
![这里写图片描述](https://img-blog.csdn.net/20180212182304175?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29zaGlsaWppdXlp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值