大佬教程收集整理的这篇文章主要介绍了Spring Boot MVC-不支持内容类型'application / json; charset = UTF-8',大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您不能将Collection, Map, Array or enumeration
用作@JsonBACkReference
。
尝试互换@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 之一(并更改了id
和itemname
)
// 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,请注明来意。