我对一个愚蠢的错误感到沮丧,我试图解析一个json到我想要的类,但不知何故,它返回了一个空对象。我的json如下:
{
"service_requests": [
{
"serviceRequestItems": [
{
"entityId": "688210985",
"entityType": "wh_inventory",
"sourceArea": "transient",
"destinationArea": "store",
"srItemLabels": [
],
"attributes": [
{
"name": "wid",
"value": "test"
}
]
}
]
}
]
}
这是从一个API调用返回的,我已经在IDE的调试模式下验证了它。
@JsonIgnoreProperties(ignoreUnknown = true)
public class PayloadDTO {
@JsonProperty("service_requests")
List<SRCreationRequest> serviceRequests;
public List<SRCreationRequest> getServiceRequests() {
return serviceRequests;
}
public void setServiceRequests(List<SRCreationRequest> serviceRequests) {
this.serviceRequests = serviceRequests;
}
@Override
public String toString() {
return "PayloadDTO{" + "service_requests=" + serviceRequests + '}';
}
}
public class SRCreationRequest {
private List<SRItemCreationRequest> serviceRequestItems;
public List<SRItemCreationRequest> getServiceRequestItems() {
return serviceRequestItems;
}
public void setServiceRequestItems(List<SRItemCreationRequest> serviceRequestItems) {
this.serviceRequestItems = serviceRequestItems;
}
@Override
public String toString() {
return "SRCreationRequest{" + "serviceRequestItems=" + serviceRequestItems + ", tenantId='" + tenantId + '\''
+ ", clientId='" + clientId + '\'' + ", facilityId='" + facilityId + '\'' + ", context='" + context + '\''
+ ", idempotenceKey='" + idempotenceKey + '\'' + ", shardInfo=" + shardInfo + ", apiContext='" + apiContext
+ '\'' + '}';
}
}
public class SRItemCreationRequest {
private String entityId;
private String entityType;
private SRItemContainer sourceContainer;
private String sourceArea;
private SRItemContainer destinationContainer;
private String destinationArea;
private List<SRItemLabel> srItemLabels;
private String groupId;
private List<SRItemAttribute> attributes;
public String getEntityId() {
return entityId;
}
public void setEntityId(String entityId) {
this.entityId = entityId;
}
public String getEntityType() {
return entityType;
}
public void setEntityType(String entityType) {
this.entityType = entityType;
}
public SRItemContainer getSourceContainer() {
return sourceContainer;
}
public void setSourceContainer(SRItemContainer sourceContainer) {
this.sourceContainer = sourceContainer;
}
public String getSourceArea() {
return sourceArea;
}
public void setSourceArea(String sourceArea) {
this.sourceArea = sourceArea;
}
public SRItemContainer getDestinationContainer() {
return destinationContainer;
}
public void setDestinationContainer(SRItemContainer destinationContainer) {
this.destinationContainer = destinationContainer;
}
public String getDestinationArea() {
return destinationArea;
}
public void setDestinationArea(String destinationArea) {
this.destinationArea = destinationArea;
}
public List<SRItemLabel> getSrItemLabels() {
return srItemLabels;
}
public void setSrItemLabels(List<SRItemLabel> srItemLabels) {
this.srItemLabels = srItemLabels;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public List<SRItemAttribute> getAttributes() {
return attributes;
}
public void setAttributes(List<SRItemAttribute> attributes) {
this.attributes = attributes;
}
@Override
public String toString() {
return "SRItemCreationRequest{" + "entityId='" + entityId + '\'' + ", entityType='" + entityType + '\''
+ ", sourceContainer=" + sourceContainer + ", sourceArea='" + sourceArea + '\'' + ", destinationContainer="
+ destinationContainer + ", destinationArea='" + destinationArea + '\'' + ", srItemLabels=" + srItemLabels
+ ", groupId='" + groupId + '\'' + ", attributes=" + attributes + '}';
}
}
SRItemAttribute和SRItemLabel看起来都像这样
@ApiModel
public class SRItemAttribute {
@ApiModelProperty(name = "name", value = "Attribute name")
@JsonProperty(value = "name")
@NotEmpty(message = "{sr.attr.name.notnull}")
private String name;
@ApiModelProperty(name = "value", value = "Attribute value")
@JsonProperty(value = "value")
@NotEmpty(message = "{sr.attr.value.notnull}")
private String value;
public SRItemAttribute() {
}
public SRItemAttribute(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "SRAttribute{" + "name='" + name + '\'' + ", value='" + value + '\'' + '}';
}
}
由于我没有使用SRItemContainer并忽略未知属性,因此我假设这不应该是一个问题。
Gson gson = new Gson();
String jsonInString = gson.toJson(//getting from API here);
LOGGER.debug("jsonInString "+jsonInString); //as above
PayloadDTO serviceRequests = gson.fromJson(jsonInString, PayloadDTO.class);
有人能看出我哪里做错了吗?
1条答案
按热度按时间pgccezyw1#
你检查过你的第二个类
SRCreationRequest
了吗?它不需要注解吗?对于这个部分:private List<SRItemCreationRequest> serviceRequestItems;
个我认为您需要为GSON添加注解,以便将其作为API响应模型的一个属性来读取。
对于这样的模型生成,我总是使用在线工具。手动操作会在某个地方导致错误,并且对于复杂的模型很难得到。
在这里检查这个网站:http://www.jsonschema2pojo.org/
我已经生成了一个java类pojo类的json响应。
结果如下:
对于有效负载DTO:
对于服务请求:
对于服务请求项:
最后对于属性模型:
我使用的注解可能与您使用的注解略有不同,但它会根据您在右侧选项中的设置生成正确的注解。
这是我的配置为您的模型在网站: