gpt4 book ai didi

spring-boot - Spring Boot RequestMapping 不适用于 Filter

转载 作者:行者123 更新时间:2023-12-05 03:56:50 24 4
gpt4 key购买 nike

我正在开发一个SpringBoot项目,其中有@RestController和FilterRegistrationBean。我添加的过滤器有效,但是@RestController和@RequestMapping中配置的url不起作用。当我请求url时,响应码是200,但没有显示内容。虽然我删除了过滤器,但 RequestMapping 工作正常。为什么? (我访问的url是http://localhost:8080/simple)这是我的代码。SessionTokenFilter:

public class SessionTokenFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
Map<String, String[]> param_map = servletRequest.getParameterMap();
for(String param_key: param_map.keySet()){
System.out.println("Parameter name: "+param_key);
}
System.out.println("Get a request from the browser!");
}
}

AppConfiguration.java

@Configuration
public class AppConfiguration {
@Bean
public FilterRegistrationBean<SessionTokenFilter> registerFilter(){
FilterRegistrationBean filterBean = new FilterRegistrationBean();
filterBean.setFilter(new SessionTokenFilter());
filterBean.setUrlPatterns(Arrays.asList("/*"));
return filterBean;
}
}

ApiRouters.java

@RestController
public class ApiRouters {
@RequestMapping(value="/simple", method= RequestMethod.GET)
public ResponseEntity simple(){
System.out.println("Simple url matched!");
return ResponseEntity.ok().body("Ok");
}
}

和主要入口类:

@SpringBootApplication
public class ApiApplication {
public static void main(String[] args){
SpringApplication.run(ApiApplication.class);
}
}

最佳答案

原因是应该调用过滤器链。当我在过滤器类中添加代码时,一切正常。

filterChain.doFilter(servletRequest, servletResponse);

关于spring-boot - Spring Boot RequestMapping 不适用于 Filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59016031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com