没有从字符串值反序列化的字符串参数 constructor/factory 方法 ('http://localhost:8080/categories/1')

no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/categories/1')

当我尝试使用 postman 的 rest api 传递 json 请求时,我遇到了一个错误,错误日志在下面,我很乐意知道这里出了什么问题,我怀疑构造函数有问题知道我使用 AllArgsConstructor 和 NoArgsConstructor 注释编写了所有参数构造函数和默认构造函数,或者我在某处遗漏了一些注释,但老实说我不确定我去了哪里错了。

o.s.d.r.w.RepositoryRestExceptionHandler : JSON parse error: Cannot construct instance of org.sid.cinema.entities.Categorie (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/categories/1'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of org.sid.cinema.entities.Categorie (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/categories/1') at [Source: (org.apache.catalina.connector.CoyoteInputStream); line: 5, column: 17] (through reference chain: org.sid.cinema.entities.Film["categorie"])

这是我在 postman 中作为 json 请求使用此 url 写的:http://localhost:8080/films 使用 post方法

{
   "titre": "my titre8",
    "description": "my descr8",
    "realisateur": "realisateur8",
    "categorie":"http://localhost:8080/categories/1"
   
}

这是电影 class

package org.sid.cinema.entities;

import java.util.Collection;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Entity
@Data @AllArgsConstructor @NoArgsConstructor @ToString
public class Film {
    @Id @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String titre;
    private String description;
    private String realisateur;
    private Date dateSortie;
    private double duree;
    private String photo;
    @OneToMany(mappedBy="film")
    private Collection<Projection> projections;
    @ManyToOne
    private Categorie categorie;
    

}

这是分类 class

package org.sid.cinema.entities;

import java.util.Collection;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Entity
@Data @AllArgsConstructor @NoArgsConstructor @ToString
public class Categorie {
    @Id @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    @Column(length=75) // par defaut length=255
    private String name ;
    @OneToMany(mappedBy="categorie")
    private Collection<Film> films;

}

问题是类别字段是类别类型,但您收到的是字符串。

应该是:

    {
      "titre": "my titre8",
      "description": "my descr8",
      "realisateur": "realisateur8",
      "categorie": {
        "id": 1,
        "name": "XX"
      } 
    }