这是我需要的json(由Ember生成,但在服务器上运行):
{"customer":{"partyType":"jdo.party.model.Organization","name":"asdf"}}
这是我得到的json:
{"id":null,"partyType":"jdo.party.model.Company","name":"Test Company","firstName":"","lastName":""}
我需要球衣客户端发出正确的json,我不知道为什么。 客户端代码:
client = ClientBuilder.newClient();
client.register(JacksonFeature.class);
client.register(new LoggingFilter(Logger.getGlobal(),true));
resource = client.target("http://localhost:8090/crm/api").path("/customers");
CustomerDto dto = new CustomerDto(null, "jdo.party.model." + type, name, "", "");
response = resource.request()
.accept(APPLICATION_JSON)
.post(Entity.json(dto), CustomerDto.class);
I类在客户端和&amp ;;上转换为json。服务器:
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "customer")
@JsonRootName("customer")
public class CustomerDto implements Serializable {
@XmlAttribute
private UUID id;
@XmlAttribute
@NotEmpty
private String partyType;
@XmlAttribute
private String name;
@XmlAttribute
private String firstName;
@XmlAttribute
private String lastName;
public CustomerDto(Party party) {
if (party instanceof Person) {
partyType = ((Person) party).getClass().getCanonicalName();
firstName = ((Person) party).getFirstName();
lastName = ((Person) party).getLastName();
} else if (party instanceof Organization) {
partyType = ((Organization) party).getClass().getCanonicalName();
name = ((Organization) party).getName();
} else {
throw new IllegalArgumentException(
String.format("Customer must be person or Organization. %s was passed instead.",
party.getClass().getCanonicalName()));
}
id = party.getId();
}
@AssertTrue
public boolean hasName() {
return isNotBlank(name) || isNotBlank(lastName);
}
public String getPartyType() {
return partyType;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((partyType == null) ? 0 : partyType.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CustomerDto other = (CustomerDto) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (partyType == null) {
if (other.partyType != null)
return false;
} else if (!partyType.equals(other.partyType))
return false;
return true;
}
@Override
public String toString() {
return "CustomerDto [id=" + id + ", partyType=" + partyType + ", name=" + name + ", firstName=" + firstName
+ ", lastName=" + lastName + "]";
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public UUID getId() {
return id;
}
/**
*
*/
private static final long serialVersionUID = 1L;
public CustomerDto() {
super();
}
public CustomerDto(UUID id, String partyType, String name, String firstName, String lastName) {
super();
this.id = id;
this.partyType = partyType;
this.name = name;
this.firstName = firstName;
this.lastName = lastName;
}
}
答案 0 :(得分:1)
所以我们需要完成两件事:
最简单的方法是使用
注释DTO@JsonInclude(JsonInclude.Include.NON_NULL)
这将告诉杰克逊忽略空值。您还需要确保将null
作为构造函数值而不是""
(这不是同一件事)。
如果要全局设置此属性,可以在ObjectMapper
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
我们可以通过几种方式注册此ObjectMapper
以在我们的应用程序中使用
在ContextResolver
中进行配置,如here所示。然后向客户端
ContextResolver
client.register(new ObjectMapperContextResolver());
使用Jackson提供程序实例配置映射器
client.register(new JacksonJaxbJsonProvider(objectMapper, null));
我只知道一种方式,那就是配置ObjectMapper
。
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
使用的实际值由您的@JsonRootName
注释值决定。
请参阅上文,了解配置要与应用程序一起使用的映射器的方法。
注意,为了保持对Jaxb注释的支持,您可能需要使用ObjectMapper
mapper.registerModule(new JaxbAnnotationModule());
我不确定,因为使用JacksonFeature,已经支持JAXB注释。但我不知道提供ObjectMapper是否会覆盖。与明确使用JacksonJaxbJsonProvider相同。所以你可能只想测试一下。
答案 1 :(得分:0)
如果您只需要JSON中的partyType和name属性,那么为什么要注释CustomerDto.java类中的其他字段