Reputation: 2056
I have to create a REST response. The data is json formatted, and must be structured as the following :
{
"device_id" : { "downlinkData" : "deadbeefcafebabe"}
}
"device_id" has to replaced for the DeviceId, for instance:
{
"333ee" : { "downlinkData" : "deadbeefcafebabe"}
}
or
{
"9886y" : { "downlinkData" : "deadbeefcafebabe"}
}
I used http://www.jsonschema2pojo.org/ and this is the result:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"device_id"
})
public class DownlinkCallbackResponse {
@JsonProperty("device_id")
private DeviceId deviceId;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("device_id")
public DeviceId getDeviceId() {
return deviceId;
}
@JsonProperty("device_id")
public void setDeviceId(DeviceId deviceId) {
this.deviceId = deviceId;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
and
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"downlinkData"
})
public class DeviceId {
@JsonProperty("downlinkData")
private String downlinkData;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("downlinkData")
public String getDownlinkData() {
return downlinkData;
}
@JsonProperty("downlinkData")
public void setDownlinkData(String downlinkData) {
this.downlinkData = downlinkData;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
But based on this POJOs I can't set the deviceID:
DownlinkCallbackResponse downlinkCallbackResponse = new DownlinkCallbackResponse ();
DeviceId deviceId = new DeviceId();
deviceId.setDownlinkData(data);
downlinkCallbackResponse.setDeviceId(deviceId);
return new ResponseEntity<>(downlinkCallbackResponse, HttpStatus.OK);
Upvotes: 1
Views: 710
Reputation: 673
get following json string
{ "downlinkData" : "deadbeefcafebabe"}
create json object ( Lib : java-json.jar )
JSONObject obj = new JSONObject();
put above json string into json object.
obj.put("333ee", jsonString);
that will create following json string
{
"333ee" : { "downlinkData" : "deadbeefcafebabe"}
}
I hope this will help you. :-)
Upvotes: 2