Spring 执行预处理方法后未调用 Rest Controller

Spring Rest Controller not getting called after Pre Handle Method executed

我在 spring 引导应用程序中添加了一个拦截器,我正在其中进行某种验证。

我遇到的问题是,即使验证成功并且从 PreHandle 方法返回 true,我的控制器方法也没有被调用。

 @Configuration
    public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    AuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor);
    }
   }

拦截器preHandle方法如下:

@Override

public boolean preHandle(HttpServletRequest request,

                         HttpServletResponse response, Object object) throws Exception {

    String oAuthToken = request.getHeader("access_token");

    if(StringUtils.isEmpty(oAuthToken)) {

       throw new UnauthorizedUserException();

    }

    Header header = new BasicHeader("cookie","access_token="+oAuthToken);

    HttpResponse httpResponse = Utils.sendGetRequest(oAuthValidationUrl,header);

    if(httpResponse.getStatusLine().getStatusCode()!= HttpStatus.SC_OK){

        throw new UnauthorizedUserException();

    }

    return true;

}

控制器代码如下:

@PostMapping("/generateForm")public ResponseEntity<?> generateForm(@Valid @RequestBody Form form) throws BaseException {

logger.debug("______Inside Controller______");

ByteArrayOutputStream output = null;

ResponseDto resp = validateRequest(form15GHReq);

if (resp.getResponseCode().equals("ERROR")) {
 return ResponseEntity.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).body(resp);
  }

 try {
  output =      formService.genereateForm15GHPDFByteArray(form15GHReq);
 } catch (Exception e) {
  logger.error("EXception occure:{}", e.getMessage());
  resp = new ResponseDto();
  resp.setResponseCode("ERROR");
 resp.setResponseMsg(e.getMessage());
 return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON).body(resp);
 } 
 logger.debug("_______Exit    Controller______");
  return  ResponseEntity.status(HttpStatus.OK).contentType(MediaType.APPLICATION_PDF).body(output.toByteArray());
}

任何人都可以通过指出我做错了什么来帮助我。为什么即使在返回 true 后我的控制器方法也没有被调用。

谢谢,

由于您覆盖了 preHandle 方法,要将请求传递给控制器​​,您必须调用 preHandle 方法的超级实现,如下所示,

 @Configuration
 public class WebMvcConfig extends WebMvcConfigurationSupport {

 @Autowired
 CustomInterceptor customInterceptor;

 @Override
 public void addInterceptors(InterceptorRegistry registry) {

     registry.addInterceptor(customInterceptor);

 }
}

@Component
public class CustomInterceptor extends HandlerInterceptorAdapter{

 @Override
  public boolean preHandle(HttpServletRequest request,
  HttpServletResponse response, Object object) throws Exception {

  String oAuthToken = request.getHeader("access_token");

   if(StringUtils.isEmpty(oAuthToken)) {

      throw new UnauthorizedUserException();

   }

    Header header = new BasicHeader("cookie","access_token="+oAuthToken);

   HttpResponse httpResponse = 
Utils.sendGetRequest(oAuthValidationUrl,header);

  if(httpResponse.getStatusLine().getStatusCode()!= HttpStatus.SC_OK){

      throw new UnauthorizedUserException();

 }

  return super.preHandle(request, response, object);

 }

}

希望这能解决您的问题:)

我的错。代码全部正确。问题出在请求上。 Json 请求以文本形式传递。它给出了 415 error.That 尝试调用控制器时失败的原因。

感谢所有提供宝贵意见的人。