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

46scxncf  于 2022-11-08  发布在  RabbitMQ
关注(0)|答案(1)|浏览(132)

我从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"
    }
}

然后我需要Map到这个模型类:

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类中的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
        }
    ]
}

**UPDATE:**获取相关客户的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);
    }
93ze6v8z

93ze6v8z1#

您可以订阅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

相关问题