猿问

发布到 restController 时不支持内容类型

这个问题只发生在这个控制器上,因为我在模型中添加了一个自我关系。


我在其他帖子和谷歌中阅读和搜索,任何解决方案似乎都有效。


我尝试设置@JsonIdentityInfo,添加@JsonBackReference 和@JsonManagedReference,颠倒这个标签的顺序,将消耗添加到 rest 方法,但一切正常


模型:


@Entity


@JsonIdentityInfo(

  generator = ObjectIdGenerators.PropertyGenerator.class, 

  property = "code")


@Table (name = "EP7_TRS_CODIGOS")

public class Code implements Serializable {


    private static final long serialVersionUID = 2L;


    @Id

    @Column (name = "COD_CODIGO")

    private String code;


    @Column (name = "DES_CODIGO")

    private String description;


    @Column (name = "COD_TIPO")

    private String type;


    @JsonManagedReference

    @ManyToOne

    @JoinColumn (name = "ID_ORIGEN", referencedColumnName = "ID_ORIGEN")

    private Origin origin;


    @JsonBackReference

    @ManyToOne

    @JoinColumn (name="COD_CODIGO_OBJ", referencedColumnName = "COD_CODIGO")

    private Code parent;


    @JsonManagedReference

    @OneToMany (mappedBy = "parent", fetch = FetchType.LAZY)

    private List<Code> sons;

控制器:


@PostMapping(value = "/code",

    consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,

    produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})

public ResponseEntity<Code> saveOrUpdateCode (@RequestHeader HttpHeaders httpHeaders, @RequestBody Code code){

    logger.info(">>>> Entra en el controlador saveOrUpdateCode");

    if (httpHeaders == null || !authorized(httpHeaders)) {

        logger.error(">>>> Error de autentificacion");

        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);

    } 

    return new ResponseEntity<>(cs.save(code), HttpStatus.CREATED);

}

慕勒3428872
浏览 103回答 1
1回答

饮歌长啸

您需要在发送 post 请求时添加Content-type带有值的标头application/json。另外,你应该@JsonManagedReference从Origin课堂上移除。
随时随地看视频慕课网APP

相关分类

Java
我要回答