0

I found below answered question

Different names of JSON property during serialization and deserialization

Unfortunately this does not work when we use Spring Restful webservice. I am not sure what is cauisng the issue but it gives some Field abiguity exception.

What I want to do is Serialize and deserialize a field name with different names.

For e.g.

class Test {

    private String name;

    @JsonProperty("myName")
    public String getName() {
        return name;
    }

    @JsonProperty("yourName")
    public void setName(String name) {
        this.name = name;
    }
}

This does not work in Spring rest

Community
  • 1
  • 1

2 Answers2

1

You can not set @JsonProperty for both (getter & setter). You can set for the field or setter method.

But you want different name for request and response, Create two classes like this.

 class StudentResponse{
   @JsonProperty(name="student_name)
   private String name;

   //getter & setter

 }

 class StudentRequest{
   @JsonProperty(name="name)
   private String name;

   //getter & setter

 }
Damith Ganegoda
  • 4,100
  • 6
  • 37
  • 46
0

Damith is right, you seem to not be able to mark both methods within the same class, however there is a way to solve this:

First off, you will have to Create a custom deserializer (or serializer, depends on your preference).

My example object:

@JsonDeserialize(using = ObjectDeserializer.class)
public class MyObject {

private String name;

public void setName(String name) {
    this.name = name;
}

@JsonProperty("SomeOtherName")
public String getName() {
    return name;
}
}

Note, i mark the getter as the property with the first name. And I give the class a custom deserializer. Which looks like that:

public class ObjectDeserializer extends JsonDeserializer<MyObject> {


        @Override
        public MyObject deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {

            MyObject object = new MyObject();

            JsonNode node = jp.getCodec().readTree(jp);
            JsonNode jsonNode = node.get("MyCustomSerializeName");
            object.setName(jsonNode.getTextValue());

            return object;
        }

    }

This class will create my custom object and get the name of the setter field description (rather than relying on the property name).

Put together, i get:

public class DeserializeTest {

    public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        MyObject o = new MyObject();
        o.setName("Hello");

        String writeValueAsString = mapper.writeValueAsString(o);
        System.out.println(writeValueAsString);

        String jsonObj = "{\"MyCustomSerializeName\":\"Other Test\"}";

        MyObject readValue = mapper.readValue(jsonObj, MyObject.class);

        System.out.println(readValue.getName());

    }
}

And this outputs:

{"SomeOtherName":"Hello"}
Other Test

I hope that helps you.

pandaadb
  • 6,306
  • 2
  • 22
  • 41