在春季合并一个模型类中的两个对象
我已经从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);}
另一方面,我有这个服务类,谁给我了客户类中的客户对象需要添加到我的模型类中,给出特定的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);
}
I have received this json message from a queue in rabbitmq:
{
"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"
}
}
Then I need to map to this model class:
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;
}
To do this I have done this in my @RabbitListener class:
@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);}
On the other hand, I have this service class who is giving me the customer object in the Customer class that need to be added to my model class giving an specific id, as follows:
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;
});
}
So at the end my question is how can I add this object to my model class? So I can have something like this in 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
}
]
}
UPDATE:
The RestController to get the related customers is as follows:
@GetMapping("/getId/{Id}")
public Mono<CuCoPerson> getRelationById(@PathVariable Integer id, @RequestHeader(value="Authorization") String GS_AUTH_TOKEN) {
return webClientService.getCuCoPerson(id, GS_AUTH_TOKEN);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以订阅人对象和
getRelatedCustomers()
和dodto.setCustomers()
您可以将
consuster
将其更改为ressectcustomers
在peopledocumentdto
时,请在必要时。You can subscribe to Person object and
getRelatedCustomers()
and dodto.setCustomers()
You can change
customers
torelatedCustomers
inPeopleDocumentDTO
if necessary.