赞
踩
最近因为项目原因不能使用/xxx的URL
但是swagger-bootstrap-ui默认的文档访问地址是dom.html,在项目中没办法用
百度了一番,发现目前并没有什么解决方案(官网上也没找到),所以就自己扒拉源码
首先找到jar包
发现dom.html 文件在/META-INF/resources/目录下,这就简单了,把目录映射出去就完事儿(请无视跨域处理,和swagger无关)
package com.frame.easy.config.web; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.io.*; /** * 全局跨域处理 * * @author tengchong * @date 2019-03-16 */ @Configuration public class GlobalCorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { //设置允许跨域的路径 registry.addMapping("/**") //设置允许跨域请求的域名 .allowedOrigins("*") //是否允许证书 不再默认开启 .allowCredentials(true) //设置允许的方法 .allowedMethods("*") //跨域允许时间 .maxAge(3600); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/doc/**").addResourceLocations("classpath:/META-INF/resources/","classpath:/META-INF/"); registry.addResourceHandler("/doc/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
然后页面就访问到了
但是呢这时候发现左侧没数据,如图:
看下网络,其实就是这个接口没调到,这简单,写个Controller给原先的Controller套层壳就完事儿
代码:
/** * SwaggerUI接口访问 */ @Controller @ApiIgnore @RequestMapping("/doc") public static class SwaggerResourceController implements InitializingBean { @Autowired private ApiResourceController apiResourceController; @Autowired private Environment environment; @Autowired private DocumentationCache documentationCache; @Autowired private ServiceModelToSwagger2Mapper mapper; @Autowired private JsonSerializer jsonSerializer; private Swagger2Controller swagger2Controller; @Override public void afterPropertiesSet() { swagger2Controller = new Swagger2Controller(environment, documentationCache, mapper, jsonSerializer); } @RequestMapping("/swagger-resources/configuration/security") @ResponseBody public ResponseEntity<SecurityConfiguration> securityConfiguration() { return apiResourceController.securityConfiguration(); } @RequestMapping("/swagger-resources/configuration/ui") @ResponseBody public ResponseEntity<UiConfiguration> uiConfiguration() { return apiResourceController.uiConfiguration(); } @RequestMapping("/swagger-resources") @ResponseBody public ResponseEntity<List<SwaggerResource>> swaggerResources() { return apiResourceController.swaggerResources(); } @RequestMapping(value = "/v2/api-docs", method = RequestMethod.GET, produces = {"application/json", "application/hal+json"}) @ResponseBody public ResponseEntity<Json> getDocumentation( @RequestParam(value = "group", required = false) String swaggerGroup, HttpServletRequest servletRequest) { return swagger2Controller.getDocumentation(swaggerGroup, servletRequest); } }
完美(请无视某个实习生写的接口,,接口名起的无法直视)
注意访问地址 http://localhost:9080/doc/doc.html 比原先的多了个前缀doc,
收工
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。