Search code examples
javaserializationjacksonjson-serialization

Serialize jackson setting the default type for a pojo


I've a pojo as below:

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = @class)
public class Shape {

public final String abc;
public final String xyz;

}

Rest api accepts object of Shape as a parameter, wherein I've to pass object like this:

{ @class = com.test.Shape, abc = "test1", xyz = "test2"}.

I want json to be passed like

{ abc = "test1", xyz = "test2"}

Basically, I want to deserialise pojo without passing @class key.


Solution

  • I was able to solve this using by adding sprint property in config file:

    spring.jackson.mapper.USE_BASE_TYPE_AS_DEFAULT_IMPL: true
    

    Below is the reference for the same: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.spring-mvc.customize-jackson-objectmapper