DavidR
DavidR

Reputation: 6962

Spring RequestBody needs to load nested Object

I am trying to get an Object param to load in one of my classes but unable to get it to work. The outer object DeviceInfo loads up as expected but the MobileDeviceData does not due to it's being inside a Transaction object.

@RequestMapping(value="/mobile/device", method = RequestMethod.PUT)
public ResponseEntity<Object> flagDevice (@RequestBody List<DeviceInfo> deviceInfoList, @RequestHeader(value=IN_AUTH_CUSTOMER_GUID) String inAuthIdentity) {
    ... code here ...

}

For clarity, here is my simplified @Entity class:

@Entity
public class DeviceInfo implements Serializable {
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;

    private MobileDeviceReasonCode reasonCode;

    @JsonProperty("mobile-data")
    private MobileDeviceData mobileDeviceData;

    public void setMobileDeviceData(MobileDeviceData mobileDeviceData) {
        this.mobileDeviceData = mobileDeviceData;
    }
}

And my MobileDeviceData class looks like this:

@Entity
public class MobileDeviceData implements Serializable {

    @Id 
    private long deviceInfoId;

    @JsonProperty("resilient_id")
    private String resilientId;

    public long getResilientId() {
        return resilientId;
    }

    public void setResilientId(long resilientId) {
       this.resilientId = resilientId;
    }
}

The JSON coming in:

[
    {
        "reasonCode": "CHARGEBACK",
        "activityDate": 1447952509,
        "inPermId": "CUSTOMER InPermId",
        "transaction": {
            "mobile-data": {
                "resilient_id": 123
            }
        }
    }
]

Is there someway I can get it to load the MobileDeviceData object without having to create a Transaction object that wouldn't hold anything except for the MobileDeviceData class? I'm hoping there is some way I can modify the @JsonProperty to inform it that the object is inside the Transaction object.

Also, modifying the JSON is not an option. I already asked.

Upvotes: 2

Views: 10978

Answers (1)

Nikolay Rusev
Nikolay Rusev

Reputation: 4230

Please check this SO thread and see both answers. I'm suggesting the second one. It will fit into your needs but it is a too custom solution - creating your own annotation and implementation despite of this, the code will look very clean, without dummy wrapper objects. I think is a very good and elegant solution.

Upvotes: 1

Related Questions