如何将项目从 java 脚本数组转换为 java 对象 spring 启动

how to convert items from a javascript array to java objects spring boot

我想 post 一个主题,它有一个 promotorList 和一个 targetAudienceList。

当我将请求主体作为字符串获取时,我可以在 java 侧打印:


{
  "title": "qsdgqsg",
  "description": "qdfgqdg",
  "amountOfStudents": "1",
  "targetAudienceList": [
    {
      "targetAudience": {
        "targetAudienceId": 1
      }
    },
    {
      "targetAudience": {
        "targetAudienceId": 2
      }
    }
  ]
  "promotorList": [
    {
      "promotor": {
        "promotorId": 1
      }
    }
  ]
}

但是当我像这样在 java 中将请求体作为主题对象时:


    @PostMapping(path = "/create")
    public void createSubject(@RequestBody Subject subject) {
        System.out.println(subject);
        //subjectService.addSubject(subject);
    }

如果我打印它,我会得到这个:


Subject(subjectId=null, title=qsdgqsg, description=qdfgqdg, amountOfStudents=1, promotorList=[Promotor(promotorId=null, user=null, researchGroup=null)], topicList=null, targetAudienceList=[TargetAudience(targetAudienceId=null, majorCode=null, campus=null), TargetAudience(targetAudienceId=null, majorCode=null, campus=null)])

如您所见,java 为列表中的 2 个目标受众创建条目,但由于某些原因 ID 为空。

Subject.java:


@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor

public class Subject {

    @Id
    @SequenceGenerator(
            name = "subject_sequence",
            sequenceName = "subject_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "subject_sequence"
    )
    private Long subjectId;
    private String title;
    private String description;
    private int amountOfStudents;

    @ManyToMany(
            cascade = CascadeType.ALL
    )
    @JoinTable(
            name = "subject_promotor",
            joinColumns = @JoinColumn(
                    name = "subject_id",
                    referencedColumnName = "subjectId"
            ),
            inverseJoinColumns = @JoinColumn(
                    name = "promotor_id",
                    referencedColumnName = "promotorId"
            )
    )
    private List<Promotor> promotorList;


    @ManyToMany(
            cascade = CascadeType.ALL
    )
    @JoinTable(
            name = "subject_topic",
            joinColumns = @JoinColumn(
                    name = "subject_id",
                    referencedColumnName = "subjectId"
            ),
            inverseJoinColumns = @JoinColumn(
                    name = "topic_id",
                    referencedColumnName = "topicId"
            )
    )
    private List<Topic> topicList;


    @ManyToMany(
            cascade = CascadeType.ALL
    )
    @JoinTable(
            name = "subject_targetAudience",
            joinColumns = @JoinColumn(
                    name = "subject_id",
                    referencedColumnName = "subjectId"
            ),
            inverseJoinColumns = @JoinColumn(
                    name = "targetAudience_id",
                    referencedColumnName = "targetAudienceId"
            )
    )
    private List<TargetAudience> targetAudienceList;

TargetAudience.java:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TargetAudience {

    @Id
    @SequenceGenerator(
            name = "targetAudience_sequence",
            sequenceName = "targetAudience_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "targetAudience_sequence"
    )
    private Long targetAudienceId;

    // for example IW E-ICT-> industriele wetenschappen Elektronica ICT
    private String majorCode;

    @OneToOne(
            cascade = CascadeType.ALL,
            fetch = FetchType.EAGER,
            optional = true
    )
    @JoinColumn(
            name = "campus",
            referencedColumnName = "name"
    )
    private Campus campus;

}

解决方案可能很明显,但我是 spring 引导的新手,希望得到答复。 提前致谢!

您的请求 JSON 与您的数据模型不匹配。 您拥有的数据模型的正确 JSON 是:

{
    "title": "qsdgqsg",
    "description": "qdfgqdg",
    "amountOfStudents": 1,
    "targetAudienceList": [
        {
            "targetAudienceId": 1
        },
        {
            "targetAudienceId": 2
        } 
    ],
    "promotorList": [
        {
        "promotorId": 1
        }
    ]
}

当 Spring 的 objectMapper 找不到变量时,它会将其设置为 null。由于您的请求缺少正确位置的值 spring 将这些值标识为缺失。 Controller 中的 restObject 中不需要的其他数据将被忽略。

我还建议您不要直接在 RestControllers 中使用 Entity。您应该使用 DTO --> https://softwareengineering.stackexchange.com/questions/171457/what-is-the-point-of-using-dto-data-transfer-objects.