繁体   English   中英

如何在 Spring 中向对象添加列表

[英]How to add a list to an object in Spring

我有这个对象类,它有一个客户列表作为属性:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PeopleDTO {

    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;
}

使用 webclient 调用另一个微服务来检索此列表,如下所示:

public Mono<CuCoPerson> getCuCoPerson(Integer cucoId, String GS_AUTH_TOKEN) {
        WebClient webClient = WebClient.create();

        return webClient.get()
                .uri(GET_RELATION_BY_ID + cucoId)
                .header("Accept", "application/json")
                .header("Authorization", GS_AUTH_TOKEN)
                .retrieve()
                .bodyToMono(CuCoPerson.class)
                .map(cuCoPerson -> {
                    List<CustomerRelation> matches = cuCoPerson.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());
                    cuCoPerson.setRelatedCustomers(matches);
                    return cuCoPerson;
                });
    }

此方法返回一个 cucoPerson 如下:

{
    "id": 1,
        "relatedCustomers": [
            {
                "customerId": "xxx",
                "systemId": 999
            }
        ]
}

所以现在我想将此对象添加到我的 PeopleDTO 类中,但我不知道如何。 这就是我所做的儿子远(硬编码):

public PeopleDTO createPeople(Long id) {
        PeopleDTO people = new PeopleDTO();
        people.setProcessType("ONLINE");
        people.setOperation("UPDATE");
        people.setEntity("DOCUMENT");
        people.setEntityType("DOCUMENT");
        people.setIdCuco(id);
        people.setDocument(new Document());
        people.setCustomers(......);
    }

如您所见,我不知道如何在最后一行添加 Mono。

预期的结果应该是这样的:

{
    "type": "ONLINE",
    "operation": "UPDATE",
    "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
        }
    ]
}

首先,创建一个客户列表,例如:

List<Customer> customers=new ArrayList<>;

然后使用循环将所有客户一一添加到其中,然后您可以直接将其添加到您的对象中,例如

people.setCustomers(customers);

您的对象分配应类似于:

public PeopleDTO createPeople(Long id) {
        PeopleDTO people = new PeopleDTO();
        people.setProcessType("ONLINE");
        people.setOperation("UPDATE");
        people.setEntity("DOCUMENT");
        people.setEntityType("DOCUMENT");
        people.setIdCuco(id);
        people.setDocument(new Document());

        List<Customer> customers=new ArrayList<>;
//add data to customer
        people.setCustomers(customers);
    }

如何将元素添加到 Object,List 的 hashmap<object><div id="text_translate"><p> 我有一个来自 dao 的列表,我想把这个列表放在一个 HashMap&gt; 中,我的列表可以包含一个服务,它有多个参数,比如 serviceId=3。 在我的最终 HashMap 中,结果如下所示: {Service 1=[100,A],Service 2=[101,A],Service 3=[Parameter[102,B],Parameter[103,B],Parameter[104,C]]} 。</p><pre> serviceId paramId type 1 100 A 2 101 A 3 102 B 3 103 B 3 104 C</pre><p> 服务.java</p><pre> private int id; //Getters+Setters</pre><p> 参数.java</p><pre> private int id; private String type; //Getters+Setters</pre><p> 测试.java</p><pre> List result = dao.getServiceParam(); HashMap&lt;Service,List&lt;Parameter&gt;&gt; mapList = new HashMap&lt;Service, List&lt;Parameter&gt;&gt;(); if(.result;isEmpty()) { for (int i=0. i&lt; result;size(). i++) { Object[] line = (Object[])result;get(i); if ((BigDecimal) line[0]!=null) { } } }</pre></div></object>

[英]How to add element to hashmap of Object,List<Object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Spring Web 流程 1 时,如何将 object 添加到 bean 中的列表? 如何将对象添加到Java列表? 如何将对象数组添加到列表 如何在另一个列表对象内添加对象 如何将List <Future <Object >>添加到Set <Object>? 如何将元素添加到 Object,List 的 hashmap<object><div id="text_translate"><p> 我有一个来自 dao 的列表,我想把这个列表放在一个 HashMap&gt; 中,我的列表可以包含一个服务,它有多个参数,比如 serviceId=3。 在我的最终 HashMap 中,结果如下所示: {Service 1=[100,A],Service 2=[101,A],Service 3=[Parameter[102,B],Parameter[103,B],Parameter[104,C]]} 。</p><pre> serviceId paramId type 1 100 A 2 101 A 3 102 B 3 103 B 3 104 C</pre><p> 服务.java</p><pre> private int id; //Getters+Setters</pre><p> 参数.java</p><pre> private int id; private String type; //Getters+Setters</pre><p> 测试.java</p><pre> List result = dao.getServiceParam(); HashMap&lt;Service,List&lt;Parameter&gt;&gt; mapList = new HashMap&lt;Service, List&lt;Parameter&gt;&gt;(); if(.result;isEmpty()) { for (int i=0. i&lt; result;size(). i++) { Object[] line = (Object[])result;get(i); if ((BigDecimal) line[0]!=null) { } } }</pre></div></object> 如何添加数据以键入列表 <List<Object> &gt; 如何将对象列表添加到另一个列表中 如何在Spring MVC中的会话中添加和检索对象 如何添加`清单 <string> 在春季的DTO中
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM