1

I know we can ignore case for input JSON by adding property in application.yml as:

spring:
  jackson:
    mapper:
      accept_case_insensitive_properties: true

But if my POJO extends an abstract class, it is not working and my JSON is not being parsed.

My abstract class:

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "event")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Orders.class, name = "orders"),
        @JsonSubTypes.Type(value = WorkOrders.class, name = "workOrders")
})
public abstract class ElasticDocument {
// Fields and getter/setter
}

My Pojo:

@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class Orders extends ElasticDocument {
//other fields

private List<OrderLine> orderLines;

}

Input JSON which I am getting from input has different case e.g.

{
  "event": "orders", 
  "OrderNo": 12345,
  "Status": "Created",
  "CustomerZipCode": "23456",
  "CustomerFirstName": "firstname1",
  "orderType": "PHONEORDER",
  "customerLastName": "lastname1",
  "OrderLines": [
    {
      "LineName": "sample"
    }
  ]
}

My contoller method where I am using this ElasticDocument object:

@PostMapping("save")
public Orders save(@RequestBody ElasticDocument elasticDocument) {
    return elasticsearchRepository.save((Orders) elasticDocument);
}

I am using Spring-boot version 2.2.4

P3arl
  • 383
  • 3
  • 18
  • **accept_case_insensitive_properties** is used to desearialize incoming json (https://fasterxml.github.io/jackson-databind/javadoc/2.5/com/fasterxml/jackson/databind/MapperFeature.html#ACCEPT_CASE_INSENSITIVE_PROPERTIES). Is this your case? Or Do you need to create a json with **myProperty** from **MyProperty** ? – JRichardsz Feb 15 '20 at 12:41
  • Yeah this is my case. I explained the same in my question that I need to deserialize the input json to one of my abstract class implementation. – P3arl Feb 15 '20 at 13:17
  • desearialize word does not exist in your question :s. #1 try to improve your question comparing the incoming json, the pojo and null values after deserealization process. #2 use **accept_case_insensitive_properties** is mandatory for you? There are a couple of ways to your requirement – JRichardsz Feb 15 '20 at 21:14

1 Answers1

1

I think you forgot to add @type to your request JSON.@type is to identify the type of ElasticDocument being serialized.

Here is a example that i tried in my local system with minimum fields in class:

ElasticDocument.java

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({
        @JsonSubTypes.Type(value = Orders.class, name = "Orders"),
        @JsonSubTypes.Type(value = WorkOrders.class, name = "workOrders")
})
public abstract class ElasticDocument {
    private Integer docId;
    private String docName;
   // getters and setters
}

Orders.java

public class Orders extends ElasticDocument{
    private Integer orderId;
    private String orderName;
    // getters and setters
}

WorkOrders.java

public class WorkOrders extends ElasticDocument{
    private Integer workOrderId;
    private String workOrderName;
    // getters and setters
}

StackOverflowController.java

@RestController
@RequestMapping("/api/v1")
public class StackOverflowController {

    @PostMapping("/orders")
    ElasticDocument createOrder(@RequestBody ElasticDocument order){
        return order;
    }
}

When i send data like this to my endpoint (Please note the attributes name in json are lowercase)

{
    "@type":"workOrders",
    "docId":123,
    "docName":"XXXX",
    "orderid":45,
    "ordername":"shoe",
    "workorderid":324,
    "workordername":"dsf"
}

It is converted to workOrders response:

{
    "@type": "workOrders",
    "docId": 123,
    "docName": "XXXX",
    "workOrderId": 324,
    "workOrderName": "dsf"
}

And when i changed the @type to Orders in request then i will get Order response:

{
    "@type": "Orders",
    "docId": 123,
    "docName": "XXXX",
    "orderId": 45,
    "orderName": "shoe"
}
Ajit Soman
  • 3,926
  • 3
  • 22
  • 41
  • To get the type information I have added property named `event` in the json as I mentioned above ElasticDocument class in `@JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "event")`. I have updated my input json in question also. I will also try your solution and will update here. Thanks. – P3arl Feb 15 '20 at 17:20