Is @JsonProperty annotation required for accessor methods?

I have inherited the following:

import com.fasterxml.jackson.annotation.JsonProperty;
public class MyClass {
  @JsonProperty("id")
  private String id;

  @JsonProperty("id")
  public String getId(){
    ...code...
  }

  @JsonProperty("id")
  public String setId(String id) {
    ...code...
  }
}

Are duplicate JsonProperty annotations necessary for getter and setter, or would Jackson automatically handle serialization / deserialization if I only annotated a private member?

+4
source share
2 answers

In your example and with the default settings, ObjectMapperno annotations will be needed when using Jackson 1.8 or later.

(, "public int getValue()" ), seters ( "public void setValue (int v);" fields ( "public int value;" ). , , , , ( ) /.

, , (1.7 ) , , .

+4

@jsonProperty. / , , , , . .

:

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    mapper.disable(MapperFeature.AUTO_DETECT_GETTERS);

:

   mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
+2

All Articles