Spring Rest POST Json RequestBody Content type not supported

后端 未结 14 1650
粉色の甜心
粉色の甜心 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:32

    specify @JsonProperty in entity class constructor like this.

    ......
    ......
    ......
    
     @JsonCreator
     public Location(@JsonProperty("sl_no") Long sl_no, 
              @JsonProperty("location")String location,
              @JsonProperty("location_type") String 
              location_type,@JsonProperty("store_sl_no")Long store_sl_no) {
      this.sl_no = sl_no;
      this.location = location;
      this.location_type = location_type;
      this.store_sl_no = store_sl_no;
     } 
    .......
    .......
    .......
    
    0 讨论(0)
  • 2020-12-05 06:34

    I had this problem when using java 9+ modules. I had to open the module in order for the com.fasterxml.jackson.databind to access the objects with reflection. Alternatively you could only open the package where the models are located if you have one.

    0 讨论(0)
  • 2020-12-05 06:36

    In my case I had two Constructors in the bean and I had the same error. I have just deleted one of them and now the issue is fixed!

    0 讨论(0)
  • 2020-12-05 06:41

    I had the same issue when I used this as a foreign key.

    @JsonBackReference
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.DETACH},fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id")
    private User user;
    

    Then I removed @JsonBackReference annotation. After that above issue was fixed.

    0 讨论(0)
  • 2020-12-05 06:42

    Really! after spending 4 hours and insane debugging I found this very strange code at com.fasterxml.jackson.databind.deser.DeserializerCache

    if (deser == null) {
        try {
            deser = _createAndCacheValueDeserializer(ctxt, factory, type);
        } catch (Exception e) {
            return false;
        }
    }
    

    Ya, the problem was double setter.

    0 讨论(0)
  • 2020-12-05 06:44

    I had the same issue. Root cause was using custom deserializer without default constructor.

    0 讨论(0)
提交回复
热议问题