Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC jackson auto serialize?
    primarykey
    data
    text
    <p>I would like to serialize an object with jackson in spring MVC.</p> <p>I have a controller which returns an ObjectTest1 which has a property ObjectTest2.</p> <pre><code>public class ObjectTest1{ private ObjectTest2; // setters getters... } public class ObjectTest2{ private String value; // setters getters... } public @ResponseBody ObjectTest1 test() throws IOException ... </code></pre> <p>I have a mapper and I have a serializer for ObjectTest2 and I've annotated the ObjectTest1.getObjectTest2 method with @JsonSerialize(using = ObjectTest2.class).</p> <p>It works correctly! But I want to use this serializer in a lot of Object, not just in ObjectTest1. </p> <p>What should I do to avoid put annotation every getter method? Can use spring this serializer automatically for all properites which is ObjectTest2?</p> <p><strong>UPDATED:</strong></p> <p>I've already use this in my code:</p> <pre><code>&lt;mvc:annotation-driven&gt; </code></pre> <p>In ajax response Objects generated correctly as json. Maybe I should try to explain another way.</p> <p>So. I have these objects:</p> <pre><code>public class DTO{ private InnerThing innerThing; @JsonSerialize(using=ThingSerializer.class) public InnerThing getThing(){...} } public class InnerThing{ private String value; } </code></pre> <p>Generated json looks like:</p> <pre><code>{"innerThing":{"value":"something"}} </code></pre> <p>Afther when I've written a serializer, json is:</p> <pre><code>{"innerThing":"something"} </code></pre> <p>It is OK, but to get the second version of json I must annotate the getInnerThing method in DTO class with @JsonSerialize...</p> <p>I don't want to annotate all methods where I use InnerThing as a property. So my question is, can spring auto serialize every property which type is InnerThing?</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