2025年swagger2访问路径(swagger无法访问)

swagger2访问路径(swagger无法访问)package com graph oss config import org springframew beans factory Initializing import org springframew beans factory annotation Autowired import org springframew beans factory

大家好,我是讯享网,很高兴认识大家。



package com.graph.oss.config; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.core.io.ClassPathResource; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.servlet.resource.PathResourceResolver; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import org.springframework.web.util.UrlPathHelper; import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.spring.web.DocumentationCache; import springfox.documentation.spring.web.json.Json; import springfox.documentation.spring.web.json.JsonSerializer; import springfox.documentation.swagger.web.ApiResourceController; import springfox.documentation.swagger.web.SecurityConfiguration; import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.UiConfiguration; import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper; import springfox.documentation.swagger2.web.Swagger2Controller; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @Configuration @EnableSwagger2 public class SwaggerConfig {
讯享网
讯享网</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String DEFAULT_PATH = "/oss"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * SwaggerUI资源访问 * * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> servletContext * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> order * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> * </span><span style="color: rgba(128, 128, 128, 1)">@throws</span><span style="color: rgba(0, 128, 0, 1)"> Exception </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Bean </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> SimpleUrlHandlerMapping swaggerUrlHandlerMapping(ServletContext servletContext, @Value(</span>"${swagger.mapping.order:10}") <span style="color: rgba(0, 0, 255, 1)">int</span> order) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception { SimpleUrlHandlerMapping urlHandlerMapping </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SimpleUrlHandlerMapping(); Map</span>&lt;String, ResourceHttpRequestHandler&gt; urlMap = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">(); { PathResourceResolver pathResourceResolver </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PathResourceResolver(); pathResourceResolver.setAllowedLocations(</span><span style="color: rgba(0, 0, 255, 1)">new</span> ClassPathResource("META-INF/resources/webjars/"<span style="color: rgba(0, 0, 0, 1)">)); pathResourceResolver.setUrlPathHelper(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> UrlPathHelper()); ResourceHttpRequestHandler resourceHttpRequestHandler </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ResourceHttpRequestHandler(); resourceHttpRequestHandler.setLocations(Arrays.asList(</span><span style="color: rgba(0, 0, 255, 1)">new</span> ClassPathResource("META-INF/resources/webjars/"<span style="color: rgba(0, 0, 0, 1)">))); resourceHttpRequestHandler.setResourceResolvers(Arrays.asList(pathResourceResolver)); resourceHttpRequestHandler.setServletContext(servletContext); resourceHttpRequestHandler.afterPropertiesSet(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置新的路径</span> urlMap.put(DEFAULT_PATH + "/webjars/"<span style="color: rgba(0, 0, 0, 1)">, resourceHttpRequestHandler); } { PathResourceResolver pathResourceResolver </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PathResourceResolver(); pathResourceResolver.setAllowedLocations(</span><span style="color: rgba(0, 0, 255, 1)">new</span> ClassPathResource("META-INF/resources/"<span style="color: rgba(0, 0, 0, 1)">)); pathResourceResolver.setUrlPathHelper(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> UrlPathHelper()); ResourceHttpRequestHandler resourceHttpRequestHandler </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ResourceHttpRequestHandler(); resourceHttpRequestHandler.setLocations(Arrays.asList(</span><span style="color: rgba(0, 0, 255, 1)">new</span> ClassPathResource("META-INF/resources/"<span style="color: rgba(0, 0, 0, 1)">))); resourceHttpRequestHandler.setResourceResolvers(Arrays.asList(pathResourceResolver)); resourceHttpRequestHandler.setServletContext(servletContext); resourceHttpRequestHandler.afterPropertiesSet(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置新的路径</span> urlMap.put(DEFAULT_PATH + "/"<span style="color: rgba(0, 0, 0, 1)">, resourceHttpRequestHandler); } urlHandlerMapping.setUrlMap(urlMap); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调整DispatcherServlet关于SimpleUrlHandlerMapping的排序</span> 
urlHandlerMapping.setOrder(order);
 </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> urlHandlerMapping; } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * SwaggerUI接口访问 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Controller @ApiIgnore @RequestMapping(DEFAULT_PATH) </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span> SwaggerResourceController <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> InitializingBean { @Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> ApiResourceController apiResourceController; @Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Environment environment; @Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> DocumentationCache documentationCache; @Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> ServiceModelToSwagger2Mapper mapper; @Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> JsonSerializer jsonSerializer; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Swagger2Controller swagger2Controller; @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> afterPropertiesSet() { swagger2Controller </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Swagger2Controller(environment, documentationCache, mapper, jsonSerializer); } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 首页 * * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> 
// @RequestMapping // public ModelAndView index() { // ModelAndView modelAndView = new ModelAndView(“redirect:” + DEFAULT_PATH + “/swagger-ui.html”); // return modelAndView; // }
讯享网 @RequestMapping(</span>"/swagger-resources/configuration/security"<span style="color: rgba(0, 0, 0, 1)">) @ResponseBody </span><span style="color: rgba(0, 0, 255, 1)">public</span> ResponseEntity&lt;SecurityConfiguration&gt;<span style="color: rgba(0, 0, 0, 1)"> securityConfiguration() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> apiResourceController.securityConfiguration(); } @RequestMapping(</span>"/swagger-resources/configuration/ui"<span style="color: rgba(0, 0, 0, 1)">) @ResponseBody </span><span style="color: rgba(0, 0, 255, 1)">public</span> ResponseEntity&lt;UiConfiguration&gt;<span style="color: rgba(0, 0, 0, 1)"> uiConfiguration() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> apiResourceController.uiConfiguration(); } @RequestMapping(</span>"/swagger-resources"<span style="color: rgba(0, 0, 0, 1)">) @ResponseBody </span><span style="color: rgba(0, 0, 255, 1)">public</span> ResponseEntity&lt;List&lt;SwaggerResource&gt;&gt;<span style="color: rgba(0, 0, 0, 1)"> swaggerResources() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> apiResourceController.swaggerResources(); } @RequestMapping(value </span>= "/v2/api-docs", method = RequestMethod.GET, produces = {"application/json", "application/hal+json"<span style="color: rgba(0, 0, 0, 1)">}) @ResponseBody </span><span style="color: rgba(0, 0, 255, 1)">public</span> ResponseEntity&lt;Json&gt;<span style="color: rgba(0, 0, 0, 1)"> getDocumentation( @RequestParam(value </span>= "group", required = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">) String swaggerGroup, HttpServletRequest servletRequest) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> swagger2Controller.getDocumentation(swaggerGroup, servletRequest); } } 
}

讯享网

小讯
上一篇 2025-04-23 19:24
下一篇 2025-05-11 18:38

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/190201.html