【发布时间】:2019-10-23 21:44:28
【问题描述】:
我有一个父 DTO,其中有许多嵌套对象。 有没有办法可以忽略所有嵌套对象以及父 DTO 上的未知属性。
如果我在父 DTO 上添加 JsonIgnore,它会忽略父类而不是嵌套类。 为了使它工作,我还必须在所有嵌套对象上添加 JsonIgnore。
有没有一种方法可以实现这一点,而无需将其写入所有 DTO 上?
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class RegistrationRequest implements Cloneable {
private Subject subject;
private CaseDetail caseDetail;
private CaseEvent caseEvent;
private List<CaseRace> caseRaces;
private List<SubjectReference> subjectReferences;
我必须使用一个端点并将其作为有效负载传递给该端点,这就是它失败的地方。
ObjectMapper mapper = new ObjectMapper(); //TODO inject through constructor
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Properties properties = propertiesFactory.getProperties(); //TODO inject through constructor
url = properties.getProperty("regCore.patientInfo");
restclient.addHeader("userId", registrationRequest.getDataEntryPersonCtepId());
String registrationRequestInJsonString = mapper.writeValueAsString(registrationRequest);
response = restclient.put(url, registrationRequestInJsonString);
RestClient put request 是我们的自定义类:
public Response put(String url, String payload){
Builder acceptInvocationBuilder = createBuilder(url);
acceptInvocationBuilder.accept(MediaType.APPLICATION_JSON);
return acceptInvocationBuilder.put(Entity.json(payload));
}
正在消费的端点如下所示:
@Path("/patient-demography")
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update patient demography", response = RegistrationRequest.class, tags = "Registration")
public Response updatePatientDemography(Registration registration) {
它无法解组,因为它抱怨属性不匹配
Failed : HTTP error code : 400 Unrecognized field
【问题讨论】:
-
这是春季项目吗?
-
不,是 jaxrs,球衣实现