Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to change the content type in exception handler
    primarykey
    data
    text
    <p>Suppose I have a controller that serves <code>GET</code> request and returns bean to be serialized to JSON and also provides an exception handler for <code>IllegalArgumentException</code> that can be raised in service:</p> <pre><code>@RequestMapping(value = "/meta/{itemId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public MetaInformation getMetaInformation(@PathVariable int itemId) { return myService.getMetaInformation(itemId); } @ExceptionHandler(IllegalArgumentException.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) @ResponseBody public String handleIllegalArgumentException(IllegalArgumentException ex) { return ExceptionUtils.getStackTrace(ex); } </code></pre> <p>Message convertors are:</p> <pre><code>&lt;mvc:annotation-driven&gt; &lt;mvc:message-converters&gt; &lt;bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /&gt; &lt;bean class="org.springframework.http.converter.StringHttpMessageConverter" /&gt; &lt;/mvc:message-converters&gt; &lt;/mvc:annotation-driven&gt; </code></pre> <p>Now when I request the given URL in browser I see the correct JSON reply. However if exception is raised, the stringified exception is converted into JSON as well, but I would love it to be processed by <code>StringHttpMessageConverter</code> (resulting <code>text/plain</code> mime type). How can I go it?</p> <p>To make the picture more complete (and complicated), suppose I also have the following handler:</p> <pre><code>@RequestMapping(value = "/version", method = RequestMethod.GET) @ResponseBody public String getApplicationVersion() { return "1.0.12"; } </code></pre> <p>This handler allows the return string to be serialized by both <code>MappingJackson2HttpMessageConverter</code> and <code>StringHttpMessageConverter</code> depending in passed <code>Accept-type</code> by the client. The return types and values should be as following:</p> <pre> +----+---------------------+-----------------------+------------------+-------------------------------------+ | NN | URL | Accept-type | Content-type | Message converter | | | | request header | response header | | +----+---------------------+-----------------------+------------------+-------------------------------------+ | 1. | /version | text/html; */* | text/plain | StringHttpMessageConverter | | 2. | /version | application/json; */* | application/json | MappingJackson2HttpMessageConverter | | 3. | /meta/1 | text/html; */* | application/json | MappingJackson2HttpMessageConverter | | 4. | /meta/1 | application/json; */* | application/json | MappingJackson2HttpMessageConverter | | 5. | /meta/0 (exception) | text/html; */* | text/plain | StringHttpMessageConverter | | 6. | /meta/0 (exception) | application/json; */* | text/plain | StringHttpMessageConverter | +----+---------------------+-----------------------+------------------+-------------------------------------+ </pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload