用简单的 POJO 或 DTO 对象替换这个持久实体?

Replace this persistent entity with a simple POJO or DTO object?

我一直在使用声纳来测试我的代码质量,但我无法解决我的代码的漏洞问题它说我应该使用 Dto 或 pojo class 而不是实体 class 但仍然不能解决它我不知道如何将我的实体转换为 dto class。 这是我的全部 class:

@Entity
public class Mission implements Serializable {

private static final long serialVersionUID = -5369734855993305723L;


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

private String name;

private String description;

@ManyToOne
private Departement departement;

@OneToMany(mappedBy="mission")
private  List<Timesheet> timesheets;

public Mission() {
    super();
}

public Mission(String name, String description){
    this.name = name;
    this.description = description;
}


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 getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Departement getDepartement() {
    return departement;
}

public void setDepartement(Departement departement) {
    this.departement = departement;
}

public List<Timesheet> getTimesheets() {
    return timesheets;
}

public void setTimesheets(List<Timesheet> timesheets) {
    this.timesheets = timesheets;
}

这是我遇到问题的 Restcontroller 方法:

    @PostMapping("/ajouterMission")
@ResponseBody
public int ajouterMission(@RequestBody Mission mission) {
    itimesheetservice.ajouterMission(mission);
    try {
        logger.info("in ajouter Mission");
        logger.debug("Je vais commencer l'ajout");
        itimesheetservice.ajouterMission(mission);
        logger.info("out ajouter Mission");
        return mission.getId();
        }
    catch (Exception e) { logger.error("Erreur dans ajouterMission() : " , e); }
    return mission.getId();
}

这是关于声纳漏洞问题的屏幕截图,了解更多详情:enter image description here

不要将您的实体模型放入控制器方法中。您必须为 @RequestBody 创建另一个模型,例如 MissionRequestModel,然后将您的请求模型转换为您的实体

public class MissionRequestModel {
private String name;
private String description;
}
@PostMapping("/ajouterMission")
@ResponseBody
public int ajouterMission(@RequestBody MissionRequestModel missionRequestModel) {
     Mission mission = new Mission(missionRequestModel);
}