1
我必須創建一個REST響應。數據被JSON格式,並且必須被構造爲執行以下操作:JAVA json模式2 POJO
{
"device_id" : { "downlinkData" : "deadbeefcafebabe"}
}
「的device_id」 已到更換爲DEVICEID,例如:
{
"333ee" : { "downlinkData" : "deadbeefcafebabe"}
}
或
{
"9886y" : { "downlinkData" : "deadbeefcafebabe"}
}
我使用http://www.jsonschema2pojo.org/這就是結果:
@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);
}
}
和
@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);
}
}
但在此基礎上的POJO我無法設置設備ID:
DownlinkCallbackResponse downlinkCallbackResponse = new DownlinkCallbackResponse();
DeviceId deviceId = new DeviceId();
deviceId.setDownlinkData(data);
downlinkCallbackResponse.setDeviceId(deviceId);
return new ResponseEntity<>(downlinkCallbackResponse, HttpStatus.OK);
「我無法設置設備ID」你是什麼意思? – 2017-05-31 20:55:18
POJO中沒有setter來取代device_id的真實ID –
例如https://stackoverflow.com/a/39923458/180100例如 – 2017-06-01 05:11:46