Spring Rest POST Json RequestBody Content type not supported

后端 未结 14 1715
粉色の甜心
粉色の甜心 2020-12-05 05:58

When I try to post new object with post method. RequestBody could not recognize contentType. Spring is already configured and POST could work with others objects, but not th

14条回答
  •  余生分开走
    2020-12-05 06:55

    I found solution. It's was because I had 2 setter with same name but different type.

    My class had id property int that I replaced with Integer when à Hibernitify my object.

    But apparently, I forgot to remove setters and I had :

    /**
     * @param id
     *            the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
    
    /**
     * @param id
     *            the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }
    

    When I removed this setter, rest resquest work very well.

    Intead to throw unmarshalling error or reflect class error. Exception HttpMediaTypeNotSupportedException seams really strange here.

    I hope this stackoverflow could be help someone else.

    SIDE NOTE

    You can check your Spring server console for the following error message:

    Failed to evaluate Jackson deserialization for type [simple type, class your.package.ClassName]: com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "propertyname"

    Then you can be sure you are dealing with the issue mentioned above.

提交回复
热议问题