Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring boot mvc-内容类型';应用程序/json;字符集=UTF-8';不支持_Java_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

Java spring boot mvc-内容类型';应用程序/json;字符集=UTF-8';不支持

Java spring boot mvc-内容类型';应用程序/json;字符集=UTF-8';不支持,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,在中,POSTing(使用邮递员)一个新的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.HttpMediaTypeNotSupp

在中,
POST
ing(使用邮递员)一个新的
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.HttpMediaTypeNotSupportedException: 
     Content type 'application/json;charset=UTF-8' not supported
在请求正文中,我复制了从
GET
请求中获得的一个现有
(并更改了
id
itemName

我确保在
Item
类中有正确的getter和setter(因为这是一个

这里还有
Cart
类,
Item
与该类有
多对一关系

@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
}
编辑

我刚刚注意到前面似乎有另一个错误:

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.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported

感谢您的帮助

您不能将
集合、映射、数组或枚举
用作
@JsonBackReference

请参阅链接:

尝试交换
@JsonBackReference
@JsonManagedReference
。它应该能用。

我也有同样的问题。 //@JsonBackReference不重要。 删除/@JsonManagedReference,它可以工作

许多:


POST方法对两者都有效。

请共享控制器代码aswell@Rakesh发布数据时,请参见上面的
ItemController
,设置contenttype=application/json,忽略字符集并尝试。是的,我在Postman中设置的所有内容类型都是
Content Type=application/json
尝试将
id
的类型从
long
更改为
long
,以及
项目和
购物车
类中的id设置/获取器
@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.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
@JsonBackReference
@OneToMany(targetEntity=Device.class, mappedBy="detectUnit") 
private List<Device> devices;
@ManyToOne
private DetectUnit detectUnit;