list字段插入到api中,postman返回ok,在heroku中,部署的api字段为空

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

我用springjpa和angular构建了一个api rest。此api有一个列表字段“tags”,可通过postman with post方法正确发布:

但当我转到部署在heroku中的api时,tags字段是空的:

{"id":3164,"name":"Iñaki","surname":"Williams","dni":"19238191G","address":"calle almanaque,Madrid","mail":"williams@iñaqui.com","phone":"9373482382","linkedln":"https://www.linkedin.com/

在/sara-b-54b93543/“,”state“:”por validar“,”rating“:”67“,”availability“:”tardes“,”tags“:[]}]
我的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(mappedBy="expert", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    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;
    }

我的标签型号:

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

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

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(
           name = "experts_tags",
           joinColumns = {@JoinColumn(name="tag_id", referencedColumnName = "id")},
           inverseJoinColumns = {@JoinColumn(name="expert_id", referencedColumnName = "id")}
   )
    private List<Expert> expert = new ArrayList<>();

    public Tag() {
    }

expertdao中的postexpert方法:

@Override
    public Expert postExpert(Expert expert) {
         this.manager.persist(expert);
         return expert;
    }

知道为什么会这样吗??谢谢您!!!

elcex8rz

elcex8rz1#

在实体上执行@manytomany字段时,它会创建第三个表,在这两个表之间进行Map,我建议您执行以下操作:
创建一个名为 Response 此类的属性与 Expert 但是没有任何注解(它不是一个实体)
每次你有机会 Expert 示例,创建 Response 采用相同值的示例
在restcontroller上,使方法返回 Expert 类返回 Response 相反,你会用 Response 示例

public class Response {

 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;
 private List<Tag> tags = new ArrayList<>()
 ...  
 }

相关问题