0

Using Exception handler i want to return new response and break the original handler response.if CustomException is thrown from filter.

i have tried below code but handler method is not called.

private boolean istest = true;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws ServletException, IOException {

    KPICounterHelper.incrementRequestCounter(request);
    if(istest) {
        throw new CustomException("kd ex",55);
    }

    chain.doFilter(request, response);
    KPICounterHelper.incrementResponseCounter(request);


}

   //this is handler code
   @RestControllerAdvice
     public class ExceptionHandler extends ResponseEntityExceptionHandler{

@org.springframework.web.bind.annotation.ExceptionHandler(value= 
   {CustomException.class})
public CustomException handleCustom() {
    System.out.println("in handler");
    return new CustomException("kd excccc", 565);
}

      }

    //application.properties

     spring.mvc.throw-exception-if-no-handler-found=true
     spring.resources.add-mappings=fals

i want to execute handler code and return response from there. and want to execute method code after chain.doFilter()

Parmar Kamlesh
  • 151
  • 1
  • 15
  • did you debug when you throw the exception on line return new CustomException("kd excccc", 565);? is it entering? – Jonathan JOhx Dec 24 '18 at 18:49
  • yes i did but it is not entering in handler. – Parmar Kamlesh Dec 24 '18 at 18:57
  • Okay so could you change your code for this: @ExceptionHandler(value = { CustomException.class }) protected ResponseEntity handleConflict( CustomException ex, WebRequest request) { String bodyOfResponse = "This should be application specific"; new HttpHeaders(), HttpStatus.CONFLICT, request); } – Jonathan JOhx Dec 24 '18 at 19:00
  • And debug it again please... let me know what happens... – Jonathan JOhx Dec 24 '18 at 19:01

0 Answers0