我对一个愚蠢的错误感到沮丧,我试图将 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);
谁能看出我哪里出错了?
答案 0 :(得分:1)
您是否检查过名为 SRCreationRequest
的第二个类。不是也不需要注释吗?对于这部分:
private List<SRItemCreationRequest> serviceRequestItems;
我认为您需要为 GSON 添加注释以将其作为 API 响应模型的属性读取。属性 @JsonProperty("serviceRequestItems")
对于这种模型生成,我总是使用在线工具。手动执行会导致某处出错,并且很难获得复杂的模型。
在这里检查这个网站:http://www.jsonschema2pojo.org/
我已经生成了你对 java 类 pojo 类的 json 响应。
结果如下:
对于 PayloadDTO:
public class PayloadDTO {
@SerializedName("service_requests")
@Expose
private List<ServiceRequest> serviceRequests = null;
public List<ServiceRequest> getServiceRequests() {
return serviceRequests;
}
public void setServiceRequests(List<ServiceRequest> serviceRequests) {
this.serviceRequests = serviceRequests;
}
}
对于服务请求:
public class ServiceRequest {
@SerializedName("serviceRequestItems")
@Expose
private List<ServiceRequestItem> serviceRequestItems = null;
public List<ServiceRequestItem> getServiceRequestItems() {
return serviceRequestItems;
}
public void setServiceRequestItems(List<ServiceRequestItem> serviceRequestItems) {
this.serviceRequestItems = serviceRequestItems;
}
}
对于 ServiceRequestItem:
public class ServiceRequestItem {
@SerializedName("entityId")
@Expose
private String entityId;
@SerializedName("entityType")
@Expose
private String entityType;
@SerializedName("sourceArea")
@Expose
private String sourceArea;
@SerializedName("destinationArea")
@Expose
private String destinationArea;
@SerializedName("srItemLabels")
@Expose
private List<Object> srItemLabels = null;
@SerializedName("attributes")
@Expose
private List<Attribute> attributes = null;
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 String getSourceArea() {
return sourceArea;
}
public void setSourceArea(String sourceArea) {
this.sourceArea = sourceArea;
}
public String getDestinationArea() {
return destinationArea;
}
public void setDestinationArea(String destinationArea) {
this.destinationArea = destinationArea;
}
public List<Object> getSrItemLabels() {
return srItemLabels;
}
public void setSrItemLabels(List<Object> srItemLabels) {
this.srItemLabels = srItemLabels;
}
public List<Attribute> getAttributes() {
return attributes;
}
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}
}
最后是属性模型:
public class Attribute {
@SerializedName("name")
@Expose
private String name;
@SerializedName("value")
@Expose
private String 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;
}
}
我使用的注释可能与您使用的略有不同,但它会根据您在右侧选项中的设置生成正确的。
这是我在站点中为您的模型进行的配置: