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