Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>headers.getMediaType() responds with the media type of the Entity, not the Accept header. The appropriate way to convert the exception is with the Accept header so your client gets the response in the format they requested. Given the above solution, if your request looks like the following (note JSON accept header, but XML Entity), you will get XML back.</p> <pre> POST http://localhost:8080/service/giftcard/invoice?draft=true HTTP/1.1 Accept: application/json Authorization: Basic dXNlcjp1c2Vy Content-Type: application/xml User-Agent: Jakarta Commons-HttpClient/3.1 Host: localhost:8080 Proxy-Connection: Keep-Alive Content-Length: 502 &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;&lt;sample&gt;&lt;node1&gt;&lt;/node1&gt;&lt;/sample&gt; </pre> <p>The correct implementation is again, to use the Accept header:</p> <pre><code>public Response toResponse(final CustomException e) { LOGGER.debug("Mapping CustomException with status + \"" + e.getStatus() + "\" and message: \"" + e.getMessage() + "\""); ResponseBuilder rb = Response.status(e.getStatus()).entity( new ErrorResponseConverter(e.getMessage(), e.getReason(), e.getErrorCode())); List&lt;MediaType&gt; accepts = headers.getAcceptableMediaTypes(); if (accepts!=null &amp;&amp; accepts.size() &gt; 0) { //just pick the first one MediaType m = accepts.get(0); LOGGER.debug("Setting response type to " + m); rb = rb.type(m); } else { //if not specified, use the entity type rb = rb.type(headers.getMediaType()); // set the response type to the entity type. } return rb.build(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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