1
votes

I have some REST Web Services in Spring Boot (version 1.5.13)

I would like to use @ControllerAdvice to handle the exceptions thrown by the controllers.

For the API testA below, the @ControllerAdvice class is able to capture and handle the exception if id=123, however, if id is not equals to 123, my program is not able to convert the ResponseEntity to JSON and "org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation (406)" is thrown.

If I uncomment the @EnableWebMvc below, the ResponseEntity is able to return the JSON result correctly, however, the @ControllerAdvice will not be able to capture the exception.

@RestController("SpocController")
//@EnableWebMvc
@RequestMapping(value = { "/testing" })
public class TestController 
{   
    @GetMapping(value = "/testA", produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody ResponseEntity<Object> testA(@RequestParam String id) throws IOException   
    {       
        if(id.equals("123"))
        {
            throw new IOException("customized exception");
        }

        Map<String,Object> body = new HashMap<String, Object>();
        body.put("hello", "hihi");

        return new ResponseEntity<Object>(body, HttpStatus.OK);     
    }

    @GetMapping(value = "/testB", produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody String testB(@RequestParam String id) throws IOException 
    {
        if(id.equals("123"))
        {
            throw new IOException("customized exception");
        }

        return "{ \"ok\":\"test ok\" }";
    }
}

For API testB, as the return type is String, it was able to return the string in JSON, and @ControllerAdvice is also able to capture and handle the exception.

This is my ControllerAdvice:

@ControllerAdvice
public class TestExceptionHandler
{
    @ResponseBody
    @ExceptionHandler(IOException.class)
    protected ResponseEntity<Object> handleIOException(IOException ex) 
    {   
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("message", ex.getMessage());        

        return new ResponseEntity(map, HttpStatus.INTERNAL_SERVER_ERROR);       
    }   
}

This is the HttpMediaTypeNotAcceptableException (406) I mentioned above.

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:259)
    at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:208)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:113)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

1) No matter where I put the @EnableWebMvc (in @RestController, @ControllerAdvice or other classes), my @ExceptionHandler will not be able to capture any exception. Any idea why it happens?

2) I would like to get my API testA working as I really want to return a ResponseEntity type and the exception thrown by it can be captured by the @ControllerAdvice. Any advice is welcomed.

Thanks in advance.

3
Seems that you never reach the controller code. Can you please share the way you perform the get request in both cases? - akortex

3 Answers

0
votes

With @RestController,

No need to use @ResponseBody, you can view the document for more detail.

-Make sure your request mapping work and log something.

-Try using @RestControllerAdvice for @RestController

0
votes

Found the culprit, after commenting the following section of code, everything works like a charm. Anyway, thank you everyone!

public HandlerExceptionResolver handlerExceptionResolver()
{
    return new HandlerExceptionResolver()
    {
        @Override
        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception)
        {
            return null;
        }
    };
}
0
votes
  1.                     @RestController
                        @RequestMapping(value = { "/testing" })
                        public class ExceptionController {
                             @GetMapping(value = "/testA", produces = MediaType.APPLICATION_JSON_VALUE)
                                public  ResponseEntity<Object> testA(@RequestParam String id) throws IOException   
                                {       
                                    if(id.equals("123"))
                                    {
                                        throw new IOException("customized exception");
                                    }
    
                                    Map<String,Object> body = new HashMap<String, Object>();
                                    body.put("hello", "hihi");
    
                                    return new ResponseEntity<Object>(body, HttpStatus.OK);     
                                }
                             @GetMapping(value = "/testB", produces = MediaType.APPLICATION_JSON_VALUE)
                                public  String testB(@RequestParam String id) throws IOException 
                                {
                                    if(id.equals("123"))
                                    {
                                        throw new IOException("customized exception");
                                    }
    
                                    return "{ \"ok\":\"test ok\" }";
                                }
    
                        }
    
    

2.change your advice controller-:

                    package com.ad.blog.controller;

                    import java.io.IOException;
                    import java.util.Optional;

                    import org.springframework.hateoas.VndErrors;
                    import org.springframework.http.HttpStatus;
                    import org.springframework.http.ResponseEntity;
                    import org.springframework.web.bind.annotation.ExceptionHandler;
                    import org.springframework.web.bind.annotation.RequestMapping;

                    @org.springframework.web.bind.annotation.ControllerAdvice
                    @RequestMapping(produces = "application/vnd.error+json")
                    public class ControllerAdvice {
                        @ExceptionHandler(IOException.class) public ResponseEntity < VndErrors > notFoundException(final IOException e) {
                            return error(e, HttpStatus.NOT_FOUND, e.getMessage());
                        }
                        private ResponseEntity < VndErrors > error(final Exception exception, final HttpStatus httpStatus, final String logRef) {
                            final String message = Optional.of(exception.getMessage()).orElse(exception.getClass().getSimpleName());
                            return new ResponseEntity < > (new VndErrors(logRef, message), httpStatus);
                        }
                    }
  1. you need to add hateos dependency in your pom.xml file.
                    <dependency>
                                <groupId>org.springframework.boot</groupId>
                                <artifactId>spring-boot-starter-hateoas</artifactId>
                            </dependency>
  1. @EnableWebMVc annotation is used to enable MVC Java config that overrides the DispatcherServlet defaults configuration.if you want to override default configuration then create a class-:
        @Configuration
        @EnableWebMvc
        public class WebConfig extends WebMvcConfigurerAdapter {


        }