在这个Spring Boot项目中,POST使用(邮递员)输入新Item资源时出现错误
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.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
在请求正文中,我复制Item了从GET请求中获得的现有s 之一(并更改了id和itemName)
GET
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有many-to-one关系的班级
Cart
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
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.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
谢谢您的帮助
您不能将Collection, Map, Array or enumeration用作@JsonBackReference。
Collection, Map, Array or enumeration
@JsonBackReference
尝试互换@JsonBackReference和@JsonManagedReference。它应该工作。
@JsonManagedReference