pom依赖
<!-- swagger2 配置 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.6</version> </dependency>
swagger配置类
package com.javasm.config; import com.javasm.common.http.EnumStatus; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.builders.ResponseMessageBuilder; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.ResponseMessage; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.Arrays; import java.util.List; //访问地址:http://localhost:8088/swagger-ui.html# //使用com.github.xiaoymin依赖后 // http://localhost:8088/doc.html 原路径 @Configuration @EnableSwagger2 public class Swagger2Configuration { //api接口包扫描路径 public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.javasm"; public static final String VERSION = "1.0.0"; @Bean public Docket createRestApi() { //添加全局响应状态码 List<ResponseMessage> responseMessageList = new ArrayList<>(); Arrays.stream(EnumStatus.values()).forEach(enumStatus -> { responseMessageList.add( new ResponseMessageBuilder().code(enumStatus.getStatus()).message(enumStatus.getMessage()).responseModel( new ModelRef(enumStatus.getMessage())).build() ); }); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .globalResponseMessage(RequestMethod.GET,responseMessageList) .globalResponseMessage(RequestMethod.DELETE,responseMessageList) .globalResponseMessage(RequestMethod.POST,responseMessageList) .globalResponseMessage(RequestMethod.PUT,responseMessageList) .select() .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE)) .paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档,忽略哪些请求 .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring boot Api ") //设置文档的标题 .description("Spring boot Api 接口文档") // 设置文档的描述 .version(VERSION) // 设置文档的版本信息-> 1.0.0 Version information .termsOfServiceUrl("http://www.xxx.com") //自己设置, 设置文档的License信息->1.3 License information .build(); } }
注解使用场景
@Api(value = "用户中心评价模块", tags = {"用户中心评价模块相关接口"}) @RestController @RequestMapping("mycomments") public class MyCommentsController extends BaseController { } @ApiOperation(value = "查询商品评论", notes = "查询商品评论", httpMethod = "GET") @GetMapping("/comments") public AxiosResult comments( @ApiParam(name = "itemId", value = "商品id", required = true) @RequestParam String itemId, @ApiParam(name = "level", value = "评价等级", required = false) @RequestParam Integer level, @ApiParam(name = "page", value = "查询下一页的第几页", required = false) @RequestParam Integer page, @RequestParam String userId, @RequestBody ShopcartBO shopcartBO @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false) @RequestParam Integer pageSize) { }
TRANSLATE with x English TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back 标签:swagger,配置,responseMessageList,import,springfox,com,public,documentation From: https://www.cnblogs.com/sibuxiang-zxb/p/17243252.html