其他分享
首页 > 其他分享> > springboot + swagger2 生成api文档

springboot + swagger2 生成api文档

作者:互联网

直接贴代码:

?
12345678910<dependency>            <groupId>io.springfox</groupId>            <artifactId>springfox-swagger2</artifactId>            <version>2.8.0</version>        </dependency>        <dependency>            <groupId>io.springfox</groupId>            <artifactId>springfox-swagger-ui</artifactId>            <version>2.8.0</version>        </dependency>

  config

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import io.swagger.annotations.ApiOperation;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;  //ConditionalOnProperty 注解,是为了根据配置文件的配置来指定swagger,是否生效。如果需要生效,就在application文件中配置 swagger.enable=true@ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = "true")@Configuration@EnableSwagger2public class SwaggerConfig extends WebMvcConfigurerAdapter {     @Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {        registry.addResourceHandler("swagger-ui.html")                .addResourceLocations("classpath:/META-INF/resources/");        registry.addResourceHandler("/webjars*")                .addResourceLocations("classpath:/META-INF/resources/webjars/");    }     @Bean    public Docket createRestApi() {        return new Docket(DocumentationType.SWAGGER_2)                .forCodeGeneration(true)                .useDefaultResponseMessages(false)                .apiInfo(apiInfo())                .select()//              .apis(RequestHandlerSelectors.basePackage(""))                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))                .paths(PathSelectors.any())                .build();    }     private ApiInfo apiInfo() {        return new ApiInfoBuilder()                .title(" API文档")                .description(" API文档")                .version("1.0")                .build();    }}

  返回model

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty; /** * 封装返回数据javabean * **/@ApiModel(value = "Result",description = "通用返回对象")public class Result {    public final static String MSG_SUCCESS = "success";     @ApiModelProperty(value = "调用是否正常",name = "isok")    private boolean isok;    @ApiModelProperty(value = "接口返回msg,如果isok是false,那么msg是错误信息",name = "msg")    private String msg;    @ApiModelProperty(value = "调用成功,返回的数据",name = "data")    private Object data;     public Result() {    }     public Result(boolean isok, String msg) {        this.isok = isok;        this.msg = msg;    }     public Result(boolean isok, String msg, Object data) {        this.isok = isok;        this.msg = msg;        this.data = data;    }     public boolean isIsok() {        return isok;    }     public void setIsok(boolean isok) {        this.isok = isok;    }     public String getMsg() {        return msg;    }     public void setMsg(String msg) {        this.msg = msg;    }     public Object getData() {        return data;    }     public void setData(Object data) {        this.data = data;    }}

  controller  上注解:

@Api(value = "/XXX",tags = "查询接口")

method 上注解 示例:

?
12345@ApiOperation(value = "XXX",notes = "XXXX")    @ApiImplicitParams({            @ApiImplicitParam(name = "methodName",value = "XXX",required = true,dataType = "String",paramType = "query"),            @ApiImplicitParam(name = "params",value = "XXXXX",required = true,paramType = "body")    })
public Result selectServiceRun(String methodName,
@RequestBody Map<String, Object> params,
HttpServletRequest request) {

 

说明:

原文地址:https://www.cnblogs.com/yanqin/p/9145895.html

标签:springboot,isok,swagger2,api,value,msg,import,springfox,public
来源: https://www.cnblogs.com/jpfss/p/11438625.html