spring boot mvc - 不支持内容类型'application/json;charset = UTF-8'
Posted
技术标签:
【中文标题】spring boot mvc - 不支持内容类型\'application/json;charset = UTF-8\'【英文标题】:spring boot mvc - Content type 'application/json;charset=UTF-8' not supportedspring boot mvc - 不支持内容类型'application/json;charset = UTF-8' 【发布时间】:2018-08-06 22:03:34 【问题描述】:在this spring boot project 中,当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
在请求正文中,我复制了从GET
请求中获得的现有Item
s 之一(并更改了id
和itemName
)
// Request body:
"id": 10, // also tried without id field as it's autogenerated
"itemName": "milk",
"cart":
"id": 1
我确保我在 Item
类中拥有正确的 getter 和 setter(因为这是 known issue)
@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
关系
@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.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
感谢您的帮助
【问题讨论】:
请分享控制器代码 @Rakesh 请参见上面的ItemController
(附加)
发布数据时,设置contenttype=application/json,忽略charset,试试看。
是的,我在 Postman 中设置的所有内容都是 Content-Type=application/json
尝试将 id
的类型从 long
更改为 Long
以及 Item
和 Cart
类中 id 的设置器/获取器
【参考方案1】:
您不能将Collection, Map, Array or enumeration
用作@JsonBackReference
。
参考链接:https://fasterxml.github.io/jackson-annotations/javadoc/2.2.0/com/fasterxml/jackson/annotation/JsonBackReference.html。
尝试交换 @JsonBackReference
和 @JsonManagedReference
。它应该可以工作。
【讨论】:
【参考方案2】:我遇到了同样的问题。 //@JsonBackReference 无关紧要。 删除 //@JsonManagedReference,就可以了。
很多:
@JsonBackReference
@OneToMany(targetEntity=Device.class, mappedBy="detectUnit")
private List<Device> devices;
一个:
@ManyToOne
private DetectUnit detectUnit;
POST 方法适用于两者。
【讨论】:
是的,尽管 Jackson Javadoc 声明@JsonBackReference
不应用于集合,但仅删除 @JsonManagedReference
即可。无论如何,我建议为实体的 CRUD 的不同用例创建 DTO 类,以避免序列化和反序列化问题。以上是关于spring boot mvc - 不支持内容类型'application/json;charset = UTF-8'的主要内容,如果未能解决你的问题,请参考以下文章
HttpMediaTypeNotSupportedException:不支持内容类型“应用程序/表单数据”spring-boot
内容类型'text/plain;charset = UTF-8'在RestController类中的spring boot中不支持错误