0

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.

tez
  • 119
  • 1
  • 1
  • 14

2 Answers2

1

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

tez
  • 119
  • 1
  • 1
  • 14
0

In your Rest API,You can put @RequestBody @Validated before the Shape parameter. like

    @PostMapping("/test")
    public void api(@RequestBody @Validated Shape shape){
    todo
    }

It should be noted that @RequestBody needs to take effect under the post request

xuanzjie
  • 51
  • 7
  • This didn't work. anything else, you can suggest me to look for? – tez Aug 09 '21 at 17:30
  • I'm sorry for not giving a perfect example, you can read it again now. The following is the description of the official website, you can also read it very smoothly.https://www.baeldung.com/spring-request-response-body – xuanzjie Aug 10 '21 at 01:23
  • I'm not sure if my question is clear, edited it a bit. I don't want UI to pass "@class=classname". This solution doesn't seem to work and url you shared is doesn't has any detail related to the problem. – tez Aug 10 '21 at 02:18
  • I understand exactly what you want, and I tested it a bit, the example is indeed no problem. The @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. So you don’t need to specify the corresponding calss like "@class=classname" – xuanzjie Aug 10 '21 at 03:12