赞
踩
- private static final String DEFAULT_PATH = "/test";
-
- /**
- * SwaggerUI资源访问
- *
- * @param servletContext
- * @param order
- * @return
- * @throws Exception
- */
- @Bean
- public SimpleUrlHandlerMapping swaggerUrlHandlerMapping(ServletContext servletContext,
- @Value("${swagger.mapping.order:10}") int order) throws Exception {
- SimpleUrlHandlerMapping urlHandlerMapping = new SimpleUrlHandlerMapping();
- Map<String, ResourceHttpRequestHandler> urlMap = new HashMap<>();
- {
- PathResourceResolver pathResourceResolver = new PathResourceResolver();
- pathResourceResolver.setAllowedLocations(new ClassPathResource("META-INF/resources/webjars/"));
- pathResourceResolver.setUrlPathHelper(new UrlPathHelper());
-
- ResourceHttpRequestHandler resourceHttpRequestHandler = new ResourceHttpRequestHandler();
- resourceHttpRequestHandler.setLocations(Arrays.asList(new ClassPathResource("META-INF/resources/webjars/")));
- resourceHttpRequestHandler.setResourceResolvers(Arrays.asList(pathResourceResolver));
- resourceHttpRequestHandler.setServletContext(servletContext);
- resourceHttpRequestHandler.afterPropertiesSet();
- //设置新的路径
- urlMap.put(DEFAULT_PATH + "/webjars/**", resourceHttpRequestHandler);
- }
- {
- PathResourceResolver pathResourceResolver = new PathResourceResolver();
- pathResourceResolver.setAllowedLocations(new ClassPathResource("META-INF/resources/"));
- pathResourceResolver.setUrlPathHelper(new UrlPathHelper());
-
- ResourceHttpRequestHandler resourceHttpRequestHandler = new ResourceHttpRequestHandler();
- resourceHttpRequestHandler.setLocations(Arrays.asList(new ClassPathResource("META-INF/resources/")));
- resourceHttpRequestHandler.setResourceResolvers(Arrays.asList(pathResourceResolver));
- resourceHttpRequestHandler.setServletContext(servletContext);
- resourceHttpRequestHandler.afterPropertiesSet();
- //设置新的路径
- urlMap.put(DEFAULT_PATH + "/**", resourceHttpRequestHandler);
- }
- urlHandlerMapping.setUrlMap(urlMap);
- //调整DispatcherServlet关于SimpleUrlHandlerMapping的排序
- urlHandlerMapping.setOrder(order);
- return urlHandlerMapping;
- }
-
- /**
- * SwaggerUI接口访问
- */
- @Controller
- @ApiIgnore
- @RequestMapping(DEFAULT_PATH)
- 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);
- }
-
- /**
- * 首页
- *
- * @return
- */
- @RequestMapping
- public ModelAndView index() {
- ModelAndView modelAndView = new ModelAndView("redirect:" + DEFAULT_PATH + "/swagger-ui.html");
- return modelAndView;
- }
-
- @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);
- }
- }
最终效果:
如果编译报错,请把swagger-ui的版本升级,亲测2.9.2是没有问题的,2.6.4编译不通过
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。