SpringBoot 集成Swagger2

1.添加pom.xml
swagger2版本使用2.9.0


    io.springfox
    springfox-swagger2
    2.9.0


    io.springfox
    springfox-swagger-ui
    2.9.0

2.Swagger2Config

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author by Abbot
 * @date 2018/09/13 10:41
 **/
@Configuration
@EnableSwagger2
public class Swagger2Config {

    /**
     * 扫描注解了@ApiOperation的方法生成API接口文
     * @return
     */
    @Bean
    public Docket RestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("rest")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 扫描controller包生成API接口文档
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.abbot.blog"))//扫描的包名
                .paths(PathSelectors.any())
                .build();
    }

     /**
      * Docket 对象
      * @return
      */
     private ApiInfo apiInfo() {
         return new ApiInfoBuilder()
                 .title("SpringBoot利用swagger构建api文档")
                 .description("Rest风格的Api文档")
                 .termsOfServiceUrl("").contact(new Contact("Abbot", "", "[email protected]"))
                 .version("1.0").licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                 .build();
     }
}

3.Controller编写
为了方便 代码都一样 只为看效果

import com.abbot.blog.aop.AopSign;
import com.abbot.blog.common.BaseController;
import com.abbot.blog.common.message.ServerRequest;
import com.abbot.blog.common.message.ServerResponse;
import com.abbot.blog.common.respcode.RespHelper;
import com.abbot.blog.web.entity.reqBody.LoginReqBody;
import com.abbot.blog.web.entity.respbody.LoginRespBody;
import com.abbot.blog.web.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author by Abbot
 * @date 2018/09/13 14:21
 **/
@AopSign
@RestController
@RequestMapping("/api")
@Api(value = "用户相关", tags = "用户相关", description = "用户相关")
public class LoginController extends BaseController {

    @Autowired
    LoginService loginService;

    @ApiOperation(value = "登录", response = ServerResponse.class)
    @RequestMapping(value = "/login", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ServerResponse login(@RequestBody ServerRequest request) {
        LoginRespBody res = new LoginRespBody();
        res.setPassword("123456");
        res.setUsername("test");
        return RespHelper.serverResponseSuccess(res);
    }


    @ApiOperation(value = "注册", response = ServerResponse.class)
    @RequestMapping(value = "/register", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ServerResponse register(@RequestBody ServerRequest request) {
        LoginRespBody res = new LoginRespBody();
        res.setPassword("123456");
        res.setUsername("test");
        return RespHelper.serverResponseSuccess(res);
    }

    @ApiOperation(value = "重置密码", response = ServerResponse.class)
    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ServerResponse resetPwd (@RequestBody ServerRequest request) {
        LoginRespBody res = new LoginRespBody();
        res.setPassword("123456");
        res.setUsername("test");
        return RespHelper.serverResponseSuccess(res);
    }

    @ApiOperation(value = "获取短信验证码", response = ServerResponse.class)
    @RequestMapping(value = "/getSmsCode", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ServerResponse getSmsCode (@RequestBody ServerRequest request) {
        LoginRespBody res = new LoginRespBody();
        res.setPassword("123456");
        res.setUsername("test");
        return RespHelper.serverResponseSuccess(res);
    }
}
注:ServerResponse 是封装的统一响应对象如下,@AopSign是自己的自定义注解:

package com.abbot.blog.common.message;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author by Abbot
 * @date 2018/09/13 15:43
 **/
@ApiModel(value = "ServerResponse", description = "统一响应封装对象,所有响应抽象参数对象")
public class ServerResponse extends Message {

    /**
     * 响应状态码
     */
    @ApiModelProperty(value = "响应状态码",position =1)
    private String retCode;

    /**
     * 响应中文描述,用于提示用户
     */
    @ApiModelProperty(value = "响应中文描述,用于提示用户",position =2)
    private String retDesc;

    /**
     * 返回数据
     */
    @ApiModelProperty(value = "响应返回数据",position =3)
    private T rspBody;


    public ServerResponse(String retCode) {
        this.retCode = retCode;
    }

    public ServerResponse(T rspBody) {
        this.rspBody = rspBody;
    }

    public ServerResponse(String retCode, String retDesc, T rspBody) {
        this.retCode = retCode;
        this.retDesc = retDesc;
        this.rspBody = rspBody;
    }

    public ServerResponse(String retCode, String retDesc) {
        this.retCode = retCode;
        this.retDesc = retDesc;
    }

    public String getRetCode() {
        return retCode;
    }

    public void setRetCode(String retCode) {
        this.retCode = retCode;
    }

    public String getRetDesc() {
        return retDesc;
    }

    public void setRetDesc(String retDesc) {
        this.retDesc = retDesc;
    }

    public T getRspBody() {
        return rspBody;
    }

    public void setRspBody(T rspBody) {
        this.rspBody = rspBody;
    }

    @Override
    public String toString() {
        return "ServerResponse{" +
                "retCode='" + retCode + '\'' +
                ", retDesc='" + retDesc + '\'' +
                ", rspBody=" + rspBody +
                '}';
    }
}

4.访问地址测试
我本地自己定义的
server.port=8111
server.servlet.context-path=/swagger2
访问:http://localhost:8111/swagger2/swagger-ui.html

如下图:

swgger-ui测试

你可能感兴趣的:(SpringBoot 集成Swagger2)