Java 自定义拦截器返回,但请求不返回';控制器中的t-hit函数
我正在开发SpringBoot项目。我有一个Java 自定义拦截器返回,但请求不返回';控制器中的t-hit函数,java,spring-boot,Java,Spring Boot,我正在开发SpringBoot项目。我有一个HandlerInterceptorAdapter,在这里我有一些逻辑来拦截和检查预处理(…)中的请求数据 public class MyCustomInterceptor implements HandlerInterceptorAdapter{ // I only override the preHandle(...) @Override public boolean preHandle(HttpServletReques
HandlerInterceptorAdapter
,在这里我有一些逻辑来拦截和检查预处理(…)
中的请求数据
public class MyCustomInterceptor implements HandlerInterceptorAdapter{
// I only override the preHandle(...)
@Override
public boolean preHandle(HttpServletRequest request,HttpServletResponse response){
//my custom logic here.
// if a condition is not met, I return false, my understanding is that it would stop this request from proceeding forward
if(conditionNotMet) {
log.info("Condition NOT met, returns false!")
response.setStatus(400);
return false;
}
log.info("Condition met, returns true!")
// otherwise, returns true
return true;
}
}
在上面拦截器的preHandle()
中,我有一些自定义逻辑,然后检查是否满足某些条件,返回false以停止继续转发请求,否则返回true。我的理解是,如果我最终返回true,请求将命中相应的rest控制器
拦截器已注册且功能良好:
@Configuration
public class MyConfigurationAdapter implements WebMvcConfigurer {
@Autowired
private MyCustomInterceptor myCustomInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myCustomInterceptor);
}
}
我有一个休息控制器:
@RestController
@RequestMapping(value = "/api/myservice/")
public class MyController {
@PostMapping(value = "add_note")
public MyResponse addNote(@Valid @RequestBody AddNotePayload payload) {
// I see logs in interceptor, it returns true but I don't see this log
log.info(" Start adding note...");
...
}
URL映射到上述控制器的
addNote(…)
函数是POST-http//localhost:8080/api/myservice/add\u-note
。当我在Postman上启动此端点时,我会在拦截器的preHandle()
中看到日志,说明满足了条件,并且preHandler()
返回true
。因为它在那里返回true,所以我希望请求将被转发到控制器级别,但我没有看到MyController
的addNote()
中的日志。为什么?原因可能是什么?不要自动连接MyCustomInterceptor,只需使用新的MyCustomInterceptor()。