Note that there are some explanatory texts on larger screens.

plurals
  1. POMarshaling JSON and Generics in Java with Spring MVC
    primarykey
    data
    text
    <p>I'm trying to marshall a JSON object into a wrapper class that contains a generic object, as well as additional information about the object's signature.</p> <pre><code>public class Signable&lt;T&gt; { private T object; private String signature; public class Signable() { generateSignature(); } /* Getters and setters */ } </code></pre> <p>The wrapper class works fine as long as I construct it with the object already created, and it is able to produce the desired json</p> <pre><code>@RequestMapping(value="/test/json/return",method=RequestMethod.GET) public @ResponseBody Signable&lt;Cart&gt; getTest() { Cart cart = new Cart(); // populate cart with OrderItems ... Signable&lt;Cart&gt; sign = new Signable&lt;Cart&gt;(); sign.setObject(cart); return sign; } </code></pre> <p>which is able to generate the expected output</p> <pre><code>{ "object":{ "orderItems":[ { "id": "****", "desc": "asdlfj", "price": 25.53 } ] }, "signature":"s9d94f9f9gdfg67d8678g6s87d6f7g6"; } </code></pre> <p>Which is the format I want. However, when trying to marshal the same json generated from Signable back into Signable, I receive the following error:</p> <pre><code>java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to Cart </code></pre> <p>For some reason it's not able to determine that "object" in the json should be mapped to the type of Cart (it just defaults to the LinkedHashMap), even though it's specified in method header. </p> <pre><code>@RequestMapping(value="/test/json/post",method=RequestMethod.POST) public @ResponseBody Signable&lt;Cart&gt; postTest(@RequestBody Signable&lt;Cart&gt; sign) </code></pre> <p>Is there a way to explicitly say which types of objects you want to generate from the JSON to insert in place of a generic? </p> <p>Any help would be greatly appreciated.</p> <p>Thanks.</p>
    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.
 

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