Spring 启动消耗2休息和合并一些领域

bmvo0sr5  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(285)

我刚接触springboot,遇到了一个问题:我需要使用2个远程rest服务并合并结果。需要一些正确方法的见解。我得到了这样的东西:

{"subInventories":[
  {"OrganizationId": 0,
    "OrganizationCode":"",
    "SecondaryInventoryName":"",
    "Description":""},...{}...],
{"organizations":[
  {"OrganizationId":0,
    "OrganizationCode":"",
    "OrganizationName":"",
    "ManagementBusinessUnitId":,
    "ManagementBusinessUnitName":""}, ...{}...]}

需要把它变成这样:

{"items":[
{"OrganizationId":0,
    "OrganizationCode":"",
    "OrganizationName":"",
    "ManagementBusinessUnitId":0,
    "ManagementBusinessUnitName":"",
    "SecondaryInventoryName":"",
    "Description":""},...{}...]

得到了2个@entitys,用json字段等属性表示每个项目、组织和库存。编辑当前正在尝试获取与java8 stream()匹配的内容

@GetMapping("/manipulate")
public List<Organization> getManipulate() {
    List<Organization> organization = (List<Organization>)(Object) organizationController.getOrganization();
    List<SubInventories> subInventories = (List<SubInventories>)(Object) getSuvInventories();
    List<Organization> intersect = organization.stream().filter(o -> subInventories.stream().anyMatch(s -> s.getOrganizationId()==o.getOrganizationId()))
            .collect(Collectors.toList());
    return intersect;

}

找到这个搜索,但我有很多类,我不知道是否它会更好,只是为每个组织获得子库存,并把他们在一个Map列表,如

List<Map<String,Object> myList = new ArrayList<>();
//Loops here
Map<String,Object> a = new HashMap<>();
a.put("OrganizationID", 1231242415)...
myList.add(a)

完全迷失在正确的方法中。
编辑2
这是我正在使用的类。
组织

@Entity
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Organization implements Serializable{

    //@JsonObject("OrganizationId")
    @Id
    private Long OrganizationId;
    private Long ManagementBusinessUnitId;
    private String OrganizationCode,OrganizationName,ManagementBusinessUnitName;

    public Organization() {
    }
//getters setters
}

子存货

@Entity
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class SubInventories implements Serializable{

    @Id
    private Long OrganizationId;
    private String OrganizationCode,SecondaryInventoryName,Description;

    public SubInventories() {

    }
//getters and setters
}

要使用的 Package

@JsonAutoDetect(fieldVisibility = Visibility.ANY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Wrapper {
    //@JsonProperty("items")
    private List<Object> items;

    public Wrapper() {
    }

    public List<Object> getOrganization() {
        return items;
    }

    public void setOrganization(List<Object> organization) {
        this.items = organization;
    }

}

组织控制器

@RestController
public class OrganizationController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Environment env;

    @GetMapping("/organizations")
    public List<Object> getOrganization() {
        return getOrganizationInfo();

    }

    private List<Object> getOrganizationInfo() {
        String url = env.getProperty("web.INVENTORY_ORGANIZATIONS");
        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
        builder.queryParam("fields", "OrganizationId,OrganizationCode,OrganizationName,ManagementBusinessUnitId,ManagementBusinessUnitName");
        builder.queryParam("onlyData", "true");
        HttpHeaders headers = new HttpHeaders();
        headers.setBasicAuth(env.getProperty("authentication.name"),env.getProperty("authentication.password"));
        HttpEntity request = new HttpEntity(headers);
        ResponseEntity<Wrapper> temp = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, request, new ParameterizedTypeReference<Wrapper>() {});
        List<Object> data = temp.getBody().getOrganization();
        return data;
    }

}

子库存控制器

@RestController
public class SubInventoryController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Environment env;

    @GetMapping("/sub")
    public List<Object> getSuvInventories() {

        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("this is private :(");

        builder.queryParam("onlyData", "true");
        builder.queryParam("expand", "subinventoriesDFF");
        builder.queryParam("limit", "999999");  
        builder.queryParam("fields", "OrganizationId,OrganizationCode,SecondaryInventoryName,Description");
        HttpHeaders headers = new HttpHeaders();
        headers.setBasicAuth(env.getProperty("authentication.name"),env.getProperty("authentication.password"));
        headers.set("REST-Framework-Version", "2");
        HttpEntity request = new HttpEntity(headers);
        ResponseEntity<Wrapper> subInventories = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, request, new ParameterizedTypeReference<Wrapper>() {});
        List<Object> data = subInventories.getBody().getOrganization();
        return data;
    }

}

我现在在哪里

@RestController
public class MainController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Environment env;
    @Autowired
    private OrganizationController organizationController;
    @Autowired
    private SubInventoryController subInventoryController;

    @GetMapping("/manipulate")
    public Map<Organization, List<SubInventories>> getManipulate() {
        List<Organization> organizations = (List<Organization>)(Object) organizationController.getOrganization();
        List<SubInventories> subInventories = (List<SubInventories>)(Object) subInventoryController.getSuvInventories();
        Map<Organization,List<SubInventories>> result = new HashMap<Organization,List<SubInventories>>();
        for(Organization organization : organizations) {
            List<SubInventories> subInventoryMatched = (List<SubInventories>) subInventories.stream().filter( s -> s.getOrganizationId()== organization.getOrganizationId()).collect(Collectors.toList());
            result.put(organizations.get(0), subInventoryMatched);
        }
        return result;  
    }
}

据我所知,我需要为每个pojo创建一个 Package 类,因为响应如下所示

{
    "items": [
        {
            "OrganizationId": 1,
            "OrganizationCode": "adasd",
            "OrganizationName": "Hotel Bahía Príncipe Sunlight Costa Adeje",
            "ManagementBusinessUnitId": 131231,
            "ManagementBusinessUnitName": "asdasfdas"
        },
        {
            "OrganizationId": 2,
            "OrganizationCode": "adadas",
            "OrganizationName": "Hadasd",
            "ManagementBusinessUnitId": 1231,
            "ManagementBusinessUnitName": "aewfrqaew"
        }]}

和/或子存货

{
    "items": [
        {
            "OrganizationId": 1,
            "OrganizationCode": "asada",
            "SecondaryInventoryName": "adfasdfasdgf",
            "Description": "pub"
        },
        {
            "OrganizationId": 2,
            "OrganizationCode": "asgfrgtsdh",
            "SecondaryInventoryName": "B LOB",
            "Description": "pub2"
        }
]}

如果与object一起使用泛型,我会得到一个java.lang.classcastexception:java.util.linkedhashmap与stream()中的com.demo.model.organization不兼容。对于字段的合并,请使用另一个类来获得所需的

{
    "items": [
        {
            "OrganizationId": 1,
            "OrganizationCode": "asdas",
            "OrganizationName": "adsadasd",
            "ManagementBusinessUnitId": 1,
            "ManagementBusinessUnitName": "asdasdf",
            "SecondaryInventoryName": "sfsdfsfa",
            "Description": "pub1"
        }]}

如果我有很多pojo的话,我会上很多课

bwntbbo3

bwntbbo31#

根据您提供的信息,我假设如下:
您有两种数据类型(java类)。它们应该合并到一个java类中
您必须从不同的来源加载这些数据
没有一个班是领头羊
我可以给你提供一些示例代码。该代码基于previos采用。这会给你一个主意。这不是一个简单的复制粘贴解决方案。
首先,创建一个包含所有要包含在结果中的字段的类:

public class Matched {
  private Object fieldA;
  private Object fieldB;

// Some getter and Setter
}

基本思想是加载数据。然后找到两个对应的对象。然后对每个字段进行匹配。

public List<Matched> matchYourData() {
  // load your data
  List<DataA> dataAList = loadYourDataA();
  List<DataB> dataBList = loadYourDataB();

  List<Matched> resultList = new ArryList<>();
  for (dataA: DataA) {
    DataB dataB = dataBList.stream()
        .filter(data -> data.getId() == dataA.getId())
        .findFirst().orElseThrow();
    // Now you have your data. Let's match them.
    Matched matched = new Matched();
    matched.setFieldA(dataB.getFieldA() == dataA.getFieldA() ? doSomething() : doSomethingElse());
    // Set all your fields. Decide for everyone the matching strategy
    resultList.add(matched);
  }
  return resultList;
}

这是一个非常简单的解决方案。当然,您可以使用mapstruct之类的工具进行Map。但这取决于你的环境。

相关问题