首页 > 解决方案 > 从服务中获取错误的输出

问题描述

我正在研究逻辑,其中我必须使用 PersonDto 列表,并根据 PersonDto 中存在的规则值(私有字符串规则;)应用规则。

我有一个名为Rules的接口,它将有多个实现。但是对于这个问题,我只添加了一个名为Rule1的实现。



我还有一个RuleMapper类,负责按需提供 bean。

问题: 根据 PersonDto 中存在的规则值,我正在应用规则。在Rule1类中,我有一个修改id字段的逻辑。我正在设置作为参数获取的新id 。
最后,我将结果存储到 ArrayList 中。
但是在 ArrayList 中,所有 PersonDto 的值都是我在应用规则时传递的最后一个 id 值。

例如:

 List<String> ids = Arrays.asList("10001", "100002");
    List<PersonDto> result = new ArrayList<PersonDto>();

    persons.stream().forEach(person -> {
        ids.stream().forEach(id -> {
            System.out.println(ruleMapper.getRule(person.getRule()).applyRule(person, id));

            result.add(ruleMapper.getRule(person.getRule()).applyRule(person, id));

        });
    });

正如您在上面的代码片段中看到的,有两个 id 1000110002,但是当我存储然后打印结果时,我得到的 PersonDto 的id值为10002在所有元素中。但是当我在循环内执行 System.out.println() 时,我看到了正确的结果。


有关更多信息或重现问题,请参阅下面的完整代码:

PersonDto

package com.example.dto;

public class PersonDto {
protected int id;
protected String name;
private String rule;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}



public String getRule() {
    return rule;
}

public void setRule(String rule) {
    this.rule = rule;
}

@Override
public String toString() {
    return "PersonDto [id=" + id + ", name=" + name + ", rule=" + rule + "]";
}

}

规则界面

package com.example.service;

import com.example.dto.PersonDto;

public interface Rules {

public PersonDto applyRule(PersonDto input, String newId);

}

规则 1 实施

package com.example.service;

import org.springframework.stereotype.Service;

import com.example.dto.PersonDto;

@Service
public class Rule1 implements Rules {

@Override
public PersonDto applyRule(PersonDto input, String newIdt) {
    input.setId(Integer.parseInt(newIdt));
    return input;
}

}

规则映射器

package com.example.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service

公共类 RuleMapper {

@Autowired
private Map<String, Rules> beans;

public Rules getRule(String ruleName) {
    return beans.get(ruleName);
}

public Map<String, Rules> gelAllBeans() {
    return beans;
  }
}

控制器

package com.example.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.dto.PersonDto;
import com.example.service.RuleMapper;

@RestController
public class StudentContoller {

@Autowired
private RuleMapper ruleMapper;

@GetMapping(value = "/test")
public void saveStudent() throws Exception {
    List<String> orders = Arrays.asList("order 1", "order 2");

    List<PersonDto> persons = new ArrayList<PersonDto>();

    for (int i = 0; i < 10; i++) {
        PersonDto per = new PersonDto();
        per.setId(i);
        per.setName("John Doe_ ".concat(String.valueOf(i)));
        per.setRule("rule" + getRandomRule());
        persons.add(per);
    }


    List<String> ids = Arrays.asList("10001", "100002");
    List<PersonDto> result = new ArrayList<PersonDto>();

    persons.stream().forEach(person -> {
        ids.stream().forEach(id -> {
            System.out.println(ruleMapper.getRule(person.getRule()).applyRule(person, id));

            result.add(ruleMapper.getRule(person.getRule()).applyRule(person, id));

        });
    });


    for (PersonDto person : result) {
        System.out.println(person);
    }

}

private int getRandomRule() {
    Random r = new Random();
    int low = 1;
    int high = 2;
    int result = r.nextInt(high - low) + low;

    return result;
 }

}

预期产出

PersonDto [id=10001, name=John Doe_ 0, rule=rule1]
PersonDto [id=100002, name=John Doe_ 0, rule=rule1]
PersonDto [id=10001, name=John Doe_ 1, rule=rule1]
PersonDto [id=100002, name=John Doe_ 1, rule=rule1]
PersonDto [id=10001, name=John Doe_ 2, rule=rule1]
PersonDto [id=100002, name=John Doe_ 2, rule=rule1]
PersonDto [id=10001, name=John Doe_ 3, rule=rule1]
PersonDto [id=100002, name=John Doe_ 3, rule=rule1]
PersonDto [id=10001, name=John Doe_ 4, rule=rule1]
PersonDto [id=100002, name=John Doe_ 4, rule=rule1]
PersonDto [id=10001, name=John Doe_ 5, rule=rule1]
PersonDto [id=100002, name=John Doe_ 5, rule=rule1]
PersonDto [id=10001, name=John Doe_ 6, rule=rule1]
PersonDto [id=100002, name=John Doe_ 6, rule=rule1]
PersonDto [id=10001, name=John Doe_ 7, rule=rule1]
PersonDto [id=100002, name=John Doe_ 7, rule=rule1]
PersonDto [id=10001, name=John Doe_ 8, rule=rule1]
PersonDto [id=100002, name=John Doe_ 8, rule=rule1]
PersonDto [id=10001, name=John Doe_ 9, rule=rule1]
PersonDto [id=100002, name=John Doe_ 9, rule=rule1]

实际输出

PersonDto [id=100002, name=John Doe_ 0, rule=rule1]
PersonDto [id=100002, name=John Doe_ 0, rule=rule1]
PersonDto [id=100002, name=John Doe_ 1, rule=rule1]
PersonDto [id=100002, name=John Doe_ 1, rule=rule1]
PersonDto [id=100002, name=John Doe_ 2, rule=rule1]
PersonDto [id=100002, name=John Doe_ 2, rule=rule1]
PersonDto [id=100002, name=John Doe_ 3, rule=rule1]
PersonDto [id=100002, name=John Doe_ 3, rule=rule1]
PersonDto [id=100002, name=John Doe_ 4, rule=rule1]
PersonDto [id=100002, name=John Doe_ 4, rule=rule1]
PersonDto [id=100002, name=John Doe_ 5, rule=rule1]
PersonDto [id=100002, name=John Doe_ 5, rule=rule1]
PersonDto [id=100002, name=John Doe_ 6, rule=rule1]
PersonDto [id=100002, name=John Doe_ 6, rule=rule1]
PersonDto [id=100002, name=John Doe_ 7, rule=rule1]
PersonDto [id=100002, name=John Doe_ 7, rule=rule1]
PersonDto [id=100002, name=John Doe_ 8, rule=rule1]
PersonDto [id=100002, name=John Doe_ 8, rule=rule1]
PersonDto [id=100002, name=John Doe_ 9, rule=rule1]
PersonDto [id=100002, name=John Doe_ 9, rule=rule1]

标签: javaspringspring-bootjava-8

解决方案


@Override
public PersonDto applyRule(PersonDto input, String newIdt) {
            PersonDto p =  new PersonDto();
            //copy the value of input to p
            p.setId(Integer.parseInt(newIdt));
            return p;
}

而不是返回一个列表,你可以返回一个Map<Integer,List<PersonDto>>

Map<Integer,List<PersonDto>> dtoMap =  ids.stream()
              .flatMap(i -> persons.stream().map(p -> Rule1.applyRule(p, i)))
                .collect(Collectors.groupingBy(arr -> (Integer)arr[0]
                        ,Collectors.mapping(arr-> (PersonDto)arr[1],Collectors.toList())));
@Override
public PersonDto applyRule(PersonDto input, String newIdt) {
            Object [] dto = {Integer.parseInt(newIdt),input};
            return dto;
}

然后当你保存到数据库时

dtoMap.forEach((k,v) -> {
            v.forEach(p -> {
                p.setId(k);
                //save to db
            } );
        });

推荐阅读