我的服务有问题。此服务包含一个add方法,该方法接收作为参数的表单。
public Cheminement add(final CheminementForm cheminementForm) throws BadRequestException {
if(cheminementForm == null){
log.error("Cheminement can not be null");
throw new BadRequestException("CheminementForm can not be null");
}else if (Objects.isNull(cheminementForm.getName())){
log.error("All fields must be filled.");
throw new BadRequestException("All fields must be filled.");
}
Cheminement cheminement = Cheminement.builder().disable(false).name(cheminementForm.getName()).build();
List<CheminementEtape> cheminementEtapeList = new ArrayList<>();
if(cheminementForm.getPositionsPoste().stream().distinct().count() != cheminementForm.getPositionsPoste().size()){
throw new BadRequestException("Cannot have same positions");
}
for(int i=0; i<cheminementForm.getEtapes().size(); i++){
if(cheminementForm.getPositionsPoste().get(i) < 0 ){
throw new BadRequestException("position cannot be null");
}
cheminementEtapeList.add(CheminementEtape.builder().cheminement(cheminement).etape(cheminementForm.getEtapes().get(i)).positionPoste(cheminementForm.getPositionsPoste().get(i)).disable(false).build());
}
cheminementRepository.save(cheminement);
cheminementEtapeService.add(cheminementEtapeList);
return cheminement;
}本表格如下:
@Data
public class CheminementForm {
@NotNull(message = "{cheminement.form.name.notEmpty}")
@Size(min=2, max=30)
private String name;
@NotNull(message = "{cheminementEtape.form.etape.notEmpty}")
private List<Etape> etapes;
@NotNull(message = "{cheminementEtape.form.positionPoste.notEmpty}")
private List<Integer> positionsPoste;
}包含整数列表和路径列表。我有一个视图,它包含一个html表单,并将一个POST方法返回给一个调用此服务的控制器。
因此,在这个服务中,add方法接受表单并(通过存储库)添加它。到目前为止,一切正常。
但是,我想添加一个条件:检查职位列表不包含相同的值。我添加了条件,但不幸的是,即使没有重复的值,它也总是返回BadRequestException。我不明白。
为了检查是否存在重复项,我使用了stream.distinct.count,它应该等于我的列表。
发布于 2022-11-03 08:38:39
我发现了问题。当我没有在视图中检查空值时,我的列表包含空值。因此,在条件出现之前,我必须从列表中删除所有的空值。
https://stackoverflow.com/questions/74245896
复制相似问题