http在put请求中发送空值(angular+spring jpa应用程序)

iswrvxsc  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(381)

所以我有一个简单的springjparest应用程序。我在heroku部署的api的结构类似于:
[{“id”:1,“姓名”:“jose”,“姓氏”:“garcia”,“dni”:“,”地址“,”邮件“:”eve。holt@reqres.in“,”phone“:”650264663“,”linkedln“:”ojskdfowa“,”state“,”rating“,”availability“,”tags“:[{”id“:1,”name“:”php“},{”id“:1,”name“:”php“}
所以我想允许用户在用户标签字段中添加标签。我使用了一个put请求来发送req体中添加了新闻标签的整个用户表单。
这就是我如何用reactiveform获取新标签并推送到expert:
Maven细节:

constructor( private formBuilder: FormBuilder,private router: Router, private expertService: ExpertsService) {this.expert= this.router?.getCurrentNavigation().extras.state.expert;
  }

  ngOnInit(): void {
    this.formGroup = this.formBuilder.group({
      tags: this.formBuilder.array([])

    });

  }
  get tagsArray(): FormArray{
    return this.formGroup.get('tags') as FormArray
  }

  addTag() {

    const tags = this.formBuilder.group(
      {
        name: ['', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(2)])]
      }
    );
    this.tagsArray.push(tags);

     this.expert.tags.push(this.formGroup.value)
     this.expertService.updateExpert(this.expert).subscribe((response) => {
      if(response){
       alert('etiqueta añadida con exito');
       }else{
        alert('Error: No Token Received');
      }
     }

     )}

expert的console.log看起来还可以,红色是用户已经拥有的标签。我想用蓝色插入:

我在Maven服务中更新的方法:

updateExpert(expert){
    return this.http.put('https://serene-wave-12377.herokuapp.com/api/experts', expert);

调用返回一个ok响应,但是当我转到api时,我看到:
[{“id”:1,“姓名”:“jose”,“姓氏”:“garcia”,“dni”:“,”地址“,”邮件“:”eve。holt@reqres.in“,”phone“:”650264663“,”linkedln“:,”state“:,”rating“,”availability“:,”tags“:[{”id“:1,”name“:”php“},{”id“:1,”name“:”php“},{”id“:1,”name“:”php“},{”id“:2,”name“:”c++“},{”id“:2,”name“:”c++“},{”id“:2,”name“:”c++“},{”id“:2,“name”:“c++”},{“id”:3,“name”:null},{“id”:4,“name”:null},{“id”:5,“name”:null},{“id”:6,“name”:null},{“id”:7,“name”:null},{“id”:8,“name”:null}]
它似乎在添加标签并生成更多的id,但name是空的。
在我的 Spring jpa中,我更新如下:

public Expert updateExpert(Expert expert) {
        Expert updatedExpert = manager.find(Expert.class, expert.getId());
        updatedExpert.setName(expert.getName());
        updatedExpert.setMail(expert.getMail());
        updatedExpert.setSurname(expert.getSurname());
        updatedExpert.setPhone(expert.getPhone());
        updatedExpert.setAvailability(expert.getAvailability());
        updatedExpert.setAddress(expert.getAddress());
        updatedExpert.setDni(expert.getDni());
        updatedExpert.setLinkedln(expert.getLinkedln());
        updatedExpert.setRating(expert.getRating());
        updatedExpert.setState(expert.getState());
        updatedExpert.getTags().addAll(expert.getTags());
        manager.merge(updatedExpert);
        return updatedExpert;
    }
}

有人知道吗?我尝试了更多的组合,但似乎没有任何效果。我试过postman,它也总是插入空值。Maven和标签有多对多的关系。

c2e8gylq

c2e8gylq1#

这是我的Maven和标签模型。所有字段都已插入,但“标记”字段除外
Maven模型:

@Entity
@Table(name="experts")
public class Expert {

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

    private String name;
    private String surname;
    private String dni;
    private String address;
    private String mail;
    private String phone;
    private String linkedln;
    private String state;
    private String rating;
    private String availability;

    @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST})
    @JoinTable(
            name = "experts_tags",
            joinColumns = {@JoinColumn(name="tags_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name="experts_id", referencedColumnName = "id")}
    )
    private List<Tag> tags = new ArrayList<>();

    public Expert() {
    }

    public Expert(String name, String surname, String dni, String address, String mail, String phone, String linkedln, String state, String rating, List<Tag> tags, String availability) {
        this.name = name;
        this.surname = surname;
        this.dni = dni;
        this.address = address;
        this.mail = mail;
        this.phone = phone;
        this.linkedln = linkedln;
        this.state = state;
        this.rating = rating;
        this.tags = tags;
        this.availability = availability;
    } //getters and setters

标签型号:

@Entity
@Table(name="tags")
public class Tag {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    @ManyToMany(mappedBy="tags")
    private List<Expert> experts = new ArrayList<>();

    public Tag() {
    }  //getters and setters

相关问题