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;