0

I've looked around a lot at other questions, but I don't see any issues that are the same as mine.

I am refactoring a legacy app and implementing Spring, and I have Spring 4 running in WebSphere using a web.xml.

My controllers are annotated with @ResponseBody and they return Jackson processed JSON fine when there is not an exception, but if there is an exception, the body of the response is not JSON, and therefore, I can't handle them well in my frontend Javascript.

For example, if I use a GET on an endpoint with a which requires a POST, I expect to see JSON like this:

{
  "timestamp": 1486410302434,
  "status": 405,
  "error": "Method Not Allowed",
  "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
  "message": "Request method 'GET' not supported",
  "path": "/greeting"
}

But instead I get this:

Error 405: Request method 'GET' not supported

How can I configure my environment to return JSON when an exception occurs?

Here's my web configuration:

@Configuration
@EnableWebMvc
@ComponentScan("hello")
public class WebConfig {

    @Bean
    public ViewResolver viewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();

        resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
        resolver.setPrefix("/Pages");
        resolver.setSuffix(".jsp");

        return resolver;
    }

And my web.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
0

1 Answer 1

1

Spring's HandlerExceptionResolvers do the exception mapping. This article and this article describes the handling mechanism along many ways to handle exceptions.

You could implement your own HandlerExceptionResolver and send back the data you want.

If you only need this for one exception you could use @ControllerAdvice

@ControllerAdvice
class GlobalControllerExceptionHandler {
    @ResponseStatus(HttpStatus.NOT_ALLOWED)  // 405
    public void handleConflict() {
        // Return whatever JSON you need
    }
}

I'm not aware of a flag that could change the error output to JSON.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.