Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a <a href="https://github.com/FasterXML/jackson-databind/issues/34" rel="nofollow noreferrer">recognized bug in Jackson</a>, which has been touted (incorrectly in my opinion) as a feature. Why do I consider it a bug? Because while serialization works, deserialization definitely does not.</p> <p>In any case, valid JSON cannot be generated from your current return type, so I would recommend creating a wrapper class:</p> <pre><code>class Result&lt;T&gt; { private T data; // constructors, getters, setters } @GET @Path("/getInteger") @Produces(APPLICATION_JSON) public Result&lt;Integer&gt; getInteger() { return new Result&lt;Integer)(42); } </code></pre> <p><strong><em>Alternatively</em></strong>, you <em>can</em> elect to wrap root values, which will automatically encapsulate your data in a top level JSON object, keyed by the objects simple type name - but note that if this option is used that all generated JSON will be wrapped (not just for primitives):</p> <pre><code>final ObjectMapper mapper = new ObjectMapper() .configure(SerializationFeature.WRAP_ROOT_VALUE, true) .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true); final String serializedJson = mapper.writeValueAsString(42); final Integer deserializedVal = mapper.readValue(serializedJson, Integer.class); System.out.println(serializedJson); System.out.println("Deserialized Value: " + deserializedVal); </code></pre> <p>Output:</p> <blockquote> <p>{"Integer":42}<br> Deserialized Value: 42</p> </blockquote> <p>See <a href="https://stackoverflow.com/a/16384686/680925">this answer</a> for details on how to retrieve and configure your <code>ObjectMapper</code> instance in a JAX-RS environment.</p>
    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