Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <code>400</code> response in this instance can indicate some sort of error while unmarhsalling the <code>POST</code> body.</p> <p>Using the <code>@XMLRootElement</code> is fine. However, you might have problems getting JAXB to unmarshal to primitive types (depends on what version you've got).</p> <p>The safest way to get JAXB to unmarshal your <code>Notification</code> object is to use the <code>Integer</code> type instead of a primitive <code>int</code> type.</p> <pre><code>@XmlRootElement public class Notification { private Integer applicationId; /* and the rest... */ } </code></pre> <p>Also, you should not need to wrap the <code>Notification</code> object with <code>JAXBElement</code> if you are using the <code>@XmlRootElement</code> annotation. Try removing the <code>JAXBElement</code> wrapper:</p> <pre><code>@POST @Path("testRest") @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) public static NotificationResponse testRest(Notification n) { logger.info("yo!"); return new NotificationResponse(101, "yo"); } </code></pre> <hr> <p>If the problem persists, then you can always use a <code>MessageBodyReader</code> to unmarshal request bodies manually.</p> <p>The typical way to do this for JSON request bodies is to implement <code>MessageBodyReader</code> and use a JSON parser of your choice, like Gson or Jackson.</p> <pre><code>@Provider @Consumes("application/json") public class CustomJsonReader&lt;T&gt; implements MessageBodyReader&lt;T&gt; { @Override public boolean isReadable(Class&lt;?&gt; type, Type genericType, Annotation[] annotations,MediaType mediaType) { return true; } @Override public T readFrom(Class&lt;T&gt; type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap&lt;String, String&gt; httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { /* Convert the request body (passed in as InputStream) to a String. * Here I'm using apache commons IOUtils to convert the input stream */ StringWriter writer = new StringWriter(); IOUtils.copy(entityStream, writer, "UTF-8"); String json = writer.toString(); /* Use JSON parser to unmarshal the JSON to a complex object */ return new Gson().fromJson(json, genericType); } } </code></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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. 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