当前位置:   article > 正文

spring-boot集成swagger-ui修改默认访问路径_spring boot swagger-ui 路径重写

spring boot swagger-ui 路径重写
  1. private static final String DEFAULT_PATH = "/test";
  2. /**
  3. * SwaggerUI资源访问
  4. *
  5. * @param servletContext
  6. * @param order
  7. * @return
  8. * @throws Exception
  9. */
  10. @Bean
  11. public SimpleUrlHandlerMapping swaggerUrlHandlerMapping(ServletContext servletContext,
  12. @Value("${swagger.mapping.order:10}") int order) throws Exception {
  13. SimpleUrlHandlerMapping urlHandlerMapping = new SimpleUrlHandlerMapping();
  14. Map<String, ResourceHttpRequestHandler> urlMap = new HashMap<>();
  15. {
  16. PathResourceResolver pathResourceResolver = new PathResourceResolver();
  17. pathResourceResolver.setAllowedLocations(new ClassPathResource("META-INF/resources/webjars/"));
  18. pathResourceResolver.setUrlPathHelper(new UrlPathHelper());
  19. ResourceHttpRequestHandler resourceHttpRequestHandler = new ResourceHttpRequestHandler();
  20. resourceHttpRequestHandler.setLocations(Arrays.asList(new ClassPathResource("META-INF/resources/webjars/")));
  21. resourceHttpRequestHandler.setResourceResolvers(Arrays.asList(pathResourceResolver));
  22. resourceHttpRequestHandler.setServletContext(servletContext);
  23. resourceHttpRequestHandler.afterPropertiesSet();
  24. //设置新的路径
  25. urlMap.put(DEFAULT_PATH + "/webjars/**", resourceHttpRequestHandler);
  26. }
  27. {
  28. PathResourceResolver pathResourceResolver = new PathResourceResolver();
  29. pathResourceResolver.setAllowedLocations(new ClassPathResource("META-INF/resources/"));
  30. pathResourceResolver.setUrlPathHelper(new UrlPathHelper());
  31. ResourceHttpRequestHandler resourceHttpRequestHandler = new ResourceHttpRequestHandler();
  32. resourceHttpRequestHandler.setLocations(Arrays.asList(new ClassPathResource("META-INF/resources/")));
  33. resourceHttpRequestHandler.setResourceResolvers(Arrays.asList(pathResourceResolver));
  34. resourceHttpRequestHandler.setServletContext(servletContext);
  35. resourceHttpRequestHandler.afterPropertiesSet();
  36. //设置新的路径
  37. urlMap.put(DEFAULT_PATH + "/**", resourceHttpRequestHandler);
  38. }
  39. urlHandlerMapping.setUrlMap(urlMap);
  40. //调整DispatcherServlet关于SimpleUrlHandlerMapping的排序
  41. urlHandlerMapping.setOrder(order);
  42. return urlHandlerMapping;
  43. }
  44. /**
  45. * SwaggerUI接口访问
  46. */
  47. @Controller
  48. @ApiIgnore
  49. @RequestMapping(DEFAULT_PATH)
  50. public static class SwaggerResourceController implements InitializingBean {
  51. @Autowired
  52. private ApiResourceController apiResourceController;
  53. @Autowired
  54. private Environment environment;
  55. @Autowired
  56. private DocumentationCache documentationCache;
  57. @Autowired
  58. private ServiceModelToSwagger2Mapper mapper;
  59. @Autowired
  60. private JsonSerializer jsonSerializer;
  61. private Swagger2Controller swagger2Controller;
  62. @Override
  63. public void afterPropertiesSet() {
  64. swagger2Controller = new Swagger2Controller(environment, documentationCache, mapper, jsonSerializer);
  65. }
  66. /**
  67. * 首页
  68. *
  69. * @return
  70. */
  71. @RequestMapping
  72. public ModelAndView index() {
  73. ModelAndView modelAndView = new ModelAndView("redirect:" + DEFAULT_PATH + "/swagger-ui.html");
  74. return modelAndView;
  75. }
  76. @RequestMapping("/swagger-resources/configuration/security")
  77. @ResponseBody
  78. public ResponseEntity<SecurityConfiguration> securityConfiguration() {
  79. return apiResourceController.securityConfiguration();
  80. }
  81. @RequestMapping("/swagger-resources/configuration/ui")
  82. @ResponseBody
  83. public ResponseEntity<UiConfiguration> uiConfiguration() {
  84. return apiResourceController.uiConfiguration();
  85. }
  86. @RequestMapping("/swagger-resources")
  87. @ResponseBody
  88. public ResponseEntity<List<SwaggerResource>> swaggerResources() {
  89. return apiResourceController.swaggerResources();
  90. }
  91. @RequestMapping(value = "/v2/api-docs", method = RequestMethod.GET, produces = {"application/json", "application/hal+json"})
  92. @ResponseBody
  93. public ResponseEntity<Json> getDocumentation(
  94. @RequestParam(value = "group", required = false) String swaggerGroup,
  95. HttpServletRequest servletRequest) {
  96. return swagger2Controller.getDocumentation(swaggerGroup, servletRequest);
  97. }
  98. }

最终效果:

 

如果编译报错,请把swagger-ui的版本升级,亲测2.9.2是没有问题的,2.6.4编译不通过

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/856990
推荐阅读
相关标签
  

闽ICP备14008679号