使用springboot整合shiro后再整合swagger,及出现的URL [/swagger-ui] again. Check your ViewResolver setup!问题

本文详述了在SpringBoot项目中整合Swagger时遇到的循环视图路径问题及其解决方案,包括配置类、依赖添加、路径配置及Shiro权限管理下的资源开放策略。

 

首先,整合swagger的话很简单,这里首先说说之前出现的问题,

如果你加入了jsp及页面的跳转,这个时候很可能出现

Circular view path [swagger-ui]: would dispatch back to the current handler URL [/swagger-ui] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)或者是

这个问题是你的页面在跳转的时候出现的问题,之前我也出现这种问题,原因是如下有的人说要加下面的一个类进行一个指定位置

@SuppressWarnings("deprecation")
@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/");
//        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
    }
}

 其实,并不需要上边的那些注释位置,主要原因是我的controller中存在handler不能处理转发的路径,如下

       /**
		 * 页面转发, 显示main.jsp内置页面信息
		 */
		@RequestMapping("/commons/{path}")
		public String dispathcer(@PathVariable String path){
			return "commons/"+path;
		}
		
		/**
		 * 页面转发方法. 实现main.jsp显示
		 * @param path
		 * @return
		 */
		@RequestMapping("/{path}")
		public String toLogin(@PathVariable String path){
			return path;
		}

         在这里发生了多次的页面的跳转,导致系统不知道如何跳转才出现Hint: This may be the result of an unspecified view, due to default view name generation.这个错误,当我把这个注释掉之后就可以显示swagger-ui.html了

1、在pom.xml文件中添加swagger依赖

	    <!-- 集成swagger接口文档 -->
	    <dependency>
	        <groupId>io.springfox</groupId>
	        <artifactId>springfox-swagger2</artifactId>
	        <version>2.8.0</version>
	    </dependency>
	    <!-- swagger-ui -->
	    <dependency>
	        <groupId>io.springfox</groupId>
	        <artifactId>springfox-swagger-ui</artifactId>
	        <version>2.8.0</version>
	    </dependency>

2、编写swagger配置类

@Configuration
@EnableSwagger2
@EnableAutoConfiguration
@ComponentScan("cn.zkhh.controller")
public class Swagger2 {

	@Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.zkhh"))
                .paths(PathSelectors.any())
                .build();
    }
	
	
	 //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                //创建人
                .contact(new Contact("贾洋洋", "https://blog.youkuaiyun.com/null111666", ""))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}

3、在启动类中要配置路径,以便可以扫描到swagger,并开启swagger的注解,如图

 

4、然后启动之后,访问地址http://localhost:端口/swagger-ui.html,即可,如图

5、如果你是使用springboot也整合了shiro的权限管理控制的话,就需要开放swagger的资源路径,否则也是访问不到swagger接       口资源的,开发路径如下

        /**swagger拦截配置*/
        filterChainDefinitionMap.put("/swagger-ui.html", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**", "anon");
        filterChainDefinitionMap.put("/swagger-resources", "anon");
        filterChainDefinitionMap.put("/v2/api-docs", "anon");
        filterChainDefinitionMap.put("/webjars/springfox-swagger-ui/**", "anon");

这样,在整合shiro后依然可以访问swagger

 

使用 Spring Boot 3 集成 Swagger 时,访问 `/swagger-ui/index.html` 出现 404 错误,通常是由于静态资源路径映射问题或依赖版本不兼容导致的。以下是解决该问题的综合方案: ### 1. 确保使用兼容 Spring Boot 3 的 Swagger 依赖 Spring Boot 3 已经全面转向 Jakarta EE 9,包名从 `javax` 改为 `jakarta`,因此需要确保使用Swagger 依赖兼容 Spring Boot 3。 推荐使用 `springdoc-openapi` 而非 `springfox`,因为后者尚未完全支持 Spring Boot 3。以下是适用于 Spring Boot 3 的依赖配置: ```xml <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>1.6.14</version> </dependency> ``` 该依赖会自动引入 OpenAPI 3.0 支持,并提供 `/swagger-ui.html` 和 `/swagger-ui/index.html` 的访问能力。 ### 2. 检查静态资源路径配置 在某些情况下,由于自定义了 `WebMvcConfigurer` 或继承了 `WebMvcConfigurationSupport`,可能导致默认的静态资源路径映射被覆盖。需要确保以下配置存在或调整: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springdoc/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } } ``` 此配置确保了 `/swagger-ui/` 路径下的资源能够正确加载。 ### 3. 确认是否启用了 Swagger UI 在 Spring Boot 3 中使用 `springdoc-openapi` 时,可以通过以下配置启用并自定义 Swagger 的访问路径: ```yaml springdoc: swagger-ui: path: /swagger-ui/index.html enabled: true ``` 该配置确保 `/swagger-ui/index.html` 是有效的访问路径。 ### 4. 检查跨域(CORS)配置 如果项目中集成了 Shiro 或 Spring Security,并且存在跨域问题,需要在配置中添加合适的 CORS 支持: ```java @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/swagger-ui/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowedHeaders("*") .exposedHeaders("*"); } } ``` ### 5. 确保没有冲突的 Swagger 实现 避免同时引入 `springfox` 和 `springdoc`,因为它们功能重叠且可能导致路径冲突。建议完全移除 `springfox` 依赖,仅使用 `springdoc`。 ### 6. 检查项目上下文路径(Context Path) 如果应用配置了 `server.servlet.context-path`,例如: ```yaml server: servlet: context-path: /api ``` 则访问路径应为:`http://localhost:8080/api/swagger-ui/index.html`。确保路径中包含正确的上下文前缀。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值