程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring Boot MVC-不支持内容类型'application / json; charset = UTF-8'大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring Boot MVC-不支持内容类型'application / json; charset = UTF-8'?

开发过程中遇到Spring Boot MVC-不支持内容类型'application / json; charset = UTF-8'的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring Boot MVC-不支持内容类型'application / json; charset = UTF-8'的解决方法建议,希望对你解决Spring Boot MVC-不支持内容类型'application / json; charset = UTF-8'有所启发或帮助;

您不能将Collection, Map, Array or enumeration用作@JsonBACkReference

请参阅链接:[https](https://fasterxml.github.io/jackson-
Annotations/javadoc/2.2.0/com/fasterxml/jackson/Annotation/JsonBACkReference.HTML)
//fasterxml.github.io/jackson- Annotations/javadoc/2.2.0/com/fasterxml/jackson/Annotation/JsonBACkReference.html。

尝试互换@JsonBACkReference@JsonManagedReference。它应该工作。

解决方法

在这个Spring Boot项目中POST使用(邮递员)输入新Item资源时出现错误

Resolving exception from handler 
     [public com.example.demo.resource.Item com.example.demo.controller.ItemController.addItem(com.example.demo.resource.Item)]: 
     org.springframework.web.httpR_440_11845@ediaTypeNotSupportedException: 
     Content type 'application/json;charset=UTF-8' not supported

在请求正文中,我复制Item了从GET请求中获得的现有s 之一(并更改了iditemname

    // request body:
    {
        "id": 10,// also tried without id field as it's autogenerated
        "itemname": "milk","cart": {
            "id": 1
        }
    }

我确保Item班上有正确的吸气剂和吸气剂因为这是一个已知问题

@Entity
@Table(name="items")
@Inheritance(strategy=InheritanCEType.TABLE_PER_CLASS)
@JsonIdentityInfo(
          generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
public class Item
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @column(name = "item_id")
    private long id;

    @column(name="item_name")
    private String itemname;

    @manyToOne
    @Joincolumn(name = "cart_id",nullable=False)
    @JsonManagedReference
    private Cart cart;

   //setters and getters
}

这也是Cart与之Item有@H_620_3@many-to-one关系的班级

@Entity
@Table(name="carts")
@JsonIdentityInfo(
          generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
public class Cart 
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @column(name = "cart_id")
    private long id;

    @OneToMany(mappedBy = "cart")
    @JsonBACkReference
    private Set<Item> items;

    //setters and getters
}

这是 ItemController

@RestController
public class ItemController 
{
    private static final Logger LOG = LoggerFactory.getLogger(ItemController.class);

    @Autowired ItemDao dao;

    @GetMapping("items")
    public List<Item> getAll()
    {
        List<Item> res = new ArrayList<>();
        dao.findAll().forEach(res::add);
        return res;
    }

    @PostMapping("items")
    public Item addItem(@requestBody Item item)
    {
        return dao.save(item);
    }

    @GetMapping("items/{item_iD}")
    public Item getItemById(@PathVariable("item_id") long item_id)
    {
        Item item = dao.findById(item_id).get();
        LOG.info(" ---------------- Retrieved item: {}",item.toString());
        return item;
    }
}

编辑

我只是注意到前面似乎还有另一个错误:

Failed to evaluate Jackson deserialization for type [[simple type,class com.example.demo.resource.Item]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: CAnnot handle managed/BACk reference 'defaultReference': BACk reference type (java.util.Set) not compatible with managed type (com.example.demo.resource.Item)

这是完整的错误:

2018-02-27 11:03:09.836  WARN 9640 --- [nio-9200-exec-1] .c.j.MappingJackson2httpmessageConverter : Failed to evaluate Jackson deserialization for type [[simple type,class com.example.demo.resource.Item]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: CAnnot handle managed/BACk reference 'defaultReference': BACk reference type (java.util.Set) not compatible with managed type (com.example.demo.resource.Item)
2018-02-27 11:03:09.837  WARN 9640 --- [nio-9200-exec-1] .c.j.MappingJackson2httpmessageConverter : Failed to evaluate Jackson deserialization for type [[simple type,class com.example.demo.resource.Item]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: CAnnot handle managed/BACk reference 'defaultReference': BACk reference type (java.util.Set) not compatible with managed type (com.example.demo.resource.Item)
2018-02-27 11:03:09.838 DEBUG 9640 --- [nio-9200-exec-1] .w.s.m.m.a.ServleTinvocableHandlerMethod : Failed to resolve argument 0 of type 'com.example.demo.resource.Item'

org.springframework.web.httpR_440_11845@ediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported

谢谢您的帮助

大佬总结

以上是大佬教程为你收集整理的Spring Boot MVC-不支持内容类型'application / json; charset = UTF-8'全部内容,希望文章能够帮你解决Spring Boot MVC-不支持内容类型'application / json; charset = UTF-8'所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。