最近用springboot構建rest接口,考慮到最方便的驗證接口,想到了引入swagger。html
基本的步驟大體以下:java
1.pom中引入swagger依賴:web
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency>
2.建立swagger的配置類:spring
/** * <Description> <br> * * @author luoluocaihong<br> * @version 1.0<br> * @taskId <br> * @CreateDate Oct 24, 2016 <br> * @since V8.1<br> * @see XXXX <br> */ @Configuration @EnableWebMvc @EnableSwagger2 @ComponentScan(basePackages = { "XXXX" }) public class SwaggerConfig { /** * * Description: <br> * * @author luoluocaihong<br> * @taskId <br> * @return <br> */ ApiInfo apiInfo() { return new ApiInfoBuilder() .title("XXX Web SelfService APIs") .description("") .license("") .licenseUrl("") .termsOfServiceUrl("") .version("1.0.0") .build(); } /** * * Description: <br> * * @author luoluocaihong<br> * @taskId <br> * @return <br> */ @Bean public Docket customImplementation() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("XXXX")) .build() .directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class) .directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class) .apiInfo(apiInfo()); } }
3.添加文檔內容sql
/** * <Description> <br> * * @author luoluocaihong<br> * @version 1.0<br> * @taskId <br> * @CreateDate Jul 2, 2017 <br> * @since V8.0<br> * @see XXXX <br> */ @Api(value = "Rule Cateory") @RestController @EnableAutoConfiguration @RequestMapping(value = "/iot/ruleengine/v1/rulecatg") public class RuleCateoryController { /** * 自動注入 */ @Autowired private RuleCateoryService ruleCateoryService; @ApiOperation(value = "Query Rule Category and rule", notes = "Query Rule Category and rule", response = RuleCatgObj.class, tags = {"Rule Cateory" }) @RequestMapping(value = "", produces = {"application/json" }, method = RequestMethod.GET) @ResponseStatus(value = HttpStatus.OK) @ResponseBody public RuleCatgObj QueryRuleCategoryAndRule() { RuleCatgObj ruleCatgObj = ruleCateoryService.queryRuleCategoryAndRule(); return ruleCatgObj; } }
4.啓動springboot,訪問http://localhost:8081/swagger-ui.html
會發現頁面顯示報錯:
後臺報錯:json
2017-07-02 15:56:51.988 WARN 7176 --- [ qtp20577666-17] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/swagger-ui.html] in DispatcherServlet with name 'dispatcherServlet'api
想一下swagger-ui.html 是在springfox-swagger-ui.jar裏的
如何才能讓咱們能訪問到swagger-ui.html???
【百度到,MARK http://www.jianshu.com/p/840320d431a1】springboot
Spring Boot自動配置自己不會自動把/swagger-ui.html這個路徑映射到對應的目錄META-INF/resources/下面。咱們加上這個映射便可。app
/** * <Description> <br> * * @author luoluocaihong<br> * @version 1.0<br> * @taskId <br> * @CreateDate Jul 2, 2017 <br> * @since V8.0<br> * @see XXXX <br> */ @Configuration public class WebMVCConfig 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/");ide
} }
再次啓動springboot,訪問http://localhost:8081/swagger-ui.html,OK:
將默認訪問路徑「/」修改成「/ruleengine」 ,若是不加上下面這段代碼,訪問將有問題:
registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/");