Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you cant change your classes you can create new abstract class/interface which will be containing methods with annotation <code>@JsonIgnore</code>. In this class/interface you can define methods which <code>ObjectMapper</code> should skips during serialization/deserialization process.</p> <p>Please, see my little example:</p> <pre><code>import java.io.IOException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonProgram { public static void main(String[] args) throws IOException { Person person = new Person(); person.setId(1L); person.setName("Max"); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.addMixInAnnotations(Person.class, PersonMixIn.class); System.out.println(objectMapper.writeValueAsString(person)); } } abstract class Entity { private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } } interface Namamble { String getName(); } class Person extends Entity implements Namamble { private String name; @Override public String getName() { return name; } public void setName(String name) { this.name = name; } } interface PersonMixIn { @JsonIgnore String getName(); } </code></pre> <p><strong>EDIT - answer for the comments</strong></p> <p>You can create such mixin interface:</p> <pre><code>public static interface UserInformationMixIn { @JsonIgnore String getField3(); } </code></pre> <p>and configure <code>ObjectMapper</code> in this way:</p> <pre><code>objectMapper.addMixInAnnotations(UserInformation.class, UserInformationMixIn.class); </code></pre> <p>Full example source code:</p> <pre><code>import java.io.IOException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonProgram { public static void main(String[] args) throws IOException { UserInformation userInformation = new UserInformation(); userInformation.setField3("field3"); userInformation.setField4("field4"); userInformation.setField5("field5"); User user = new User(); user.setField1(userInformation); user.setField2("field2"); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.addMixInAnnotations(UserInformation.class, UserInformationMixIn.class); objectMapper.addMixInAnnotations(User.class, UserInformationMixIn.class); System.out.println(objectMapper.writeValueAsString(user)); } public static abstract class Someclass { String field5; public String getField5() { return field5; } public void setField5(String field5) { this.field5 = field5; } } public static class UserInformation extends Someclass { String field3; String field4; public String getField3() { return field3; } public void setField3(String field3) { this.field3 = field3; } public String getField4() { return field4; } public void setField4(String field4) { this.field4 = field4; } } public static class User { UserInformation field1; String field2; public UserInformation getField1() { return field1; } public void setField1(UserInformation field1) { this.field1 = field1; } public String getField2() { return field2; } public void setField2(String field2) { this.field2 = field2; } } public static interface UserInformationMixIn { @JsonIgnore String getField3(); @JsonIgnore String getField2(); @JsonIgnore String getField5(); } } </code></pre> <p>Helpful link:</p> <ul> <li><a href="https://stackoverflow.com/questions/7421474/how-can-i-tell-jackson-to-ignore-a-property-for-which-i-dont-have-control-over">How can I tell jackson to ignore a property for which I don't have control over the source code?</a></li> </ul>
    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.
    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