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>