在Spring中将两个对象合并为一个模型类



我从rabbitmq的队列中收到了这个json消息:

{
"type": "NEW",
"operation": "NEW",
"id": 1,
"entity": "DOCUMENT",
"entityType": "NIE",
"documents": {
"id": 1,
"additionals": {
"issuing_authority": "Spain",
"country_doc": "ES",
"place_of_birth": "",
"valid_from": "1995-08-09",
"valid_to": "0001-01-01"
},
"code": "X12345",
"typeDocument": "NIE"
}
}

我需要映射到这个模型类:

public class PeopleDocumentDTO {
private String processType;
private String operation;
private String entity;
private String entityType;
private Long id;
private Document document;
@Getter
@Setter
class Customer {
private String systemId;
private String customerId;
}
private List<Customer> customers;
}

要做到这一点,我已经在我的@RabbitListener类:

@RabbitListener(queues = "${event.queue}")
public void receivedMessage(Message message) throws JsonProcessingException {
String json = "";
json = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println(json);
logger.info("Received message: {}", json);
ObjectMapper objectMapper = new ObjectMapper();
PeopleDocumentDTO dto = objectMapper.readValue(json, PeopleDocumentDTO.class);}

另一方面,我有这个服务类,它向我提供customer类中的客户对象,需要添加到我的模型类中,并给出特定的id,如下所示:

public Mono<Person> getPerson(Integer id, String GS_AUTH_TOKEN) {
WebClient webClient = WebClient.create();
return webClient.get()
.uri(GET_RELATION_BY_ID + id)
.header("Accept", "application/json")
.header("Authorization", GS_AUTH_TOKEN)
.retrieve()
.bodyToMono(Person.class)
.map(person -> {
List<CustomerRelation> matches = person.getRelatedCustomers()
.stream()
.filter(relation -> relation.getSystemId().equals(400) || relation.getSystemId().equals(300) || relation.getSystemId().equals(410))
.filter(relation -> relation.getCustomerId().contains("F"))
.collect(Collectors.toList());
person.setRelatedCustomers(matches);
return person;
});
}

所以最后我的问题是我如何将这个对象添加到我的模型类?所以我可以在postman:

中这样写
{
"type": "NEW",
"operation": "NEW",
"id": 1,
"entity": "DOCUMENT",
"entityType": "NIE",
"documents": {
"id": 1,
"additionals": {
"issuing_authority": "Spain",
"country_doc": "ES",
"place_of_birth": "",
"valid_from": "1995-08-09",
"valid_to": "0001-01-01"
},
"code": "X12345",
"typeDocument": "NIE"
},
"id": 1,
"relatedCustomers": [
{
"customerId": "xxx",
"systemId": 999
}
]
}

更新:获取相关客户的RestController如下所示:

@GetMapping("/getId/{Id}")
public Mono<CuCoPerson> getRelationById(@PathVariable Integer id, @RequestHeader(value="Authorization") String GS_AUTH_TOKEN) {
return webClientService.getCuCoPerson(id, GS_AUTH_TOKEN);
}

您可以订阅Person对象和getRelatedCustomers(),并订阅dto.setCustomers()

@RabbitListener(queues = "${event.queue}")
public void receivedMessage(Message message) throws JsonProcessingException {
String json = "";
json = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println(json);
logger.info("Received message: {}", json);
ObjectMapper objectMapper = new ObjectMapper();
PeopleDocumentDTO dto = objectMapper.readValue(json, PeopleDocumentDTO.class);

personServie.getPerson("id", "authToken").subscribe(person -> dto.setCustomers(person.getRelatedCustomers()));

}

如果需要,可以将PeopleDocumentDTO中的customers改为relatedCustomers