HttpMediaTypeNotSupportedException:内容类型' application / json'不支持

时间:2014-05-02 15:42:35

标签: spring-mvc jackson

您好我正在使用Spring MVC。在我的控制器中,我期待表单数据

@RequestMapping(value = "/get/data/grid", method = RequestMethod.POST)
public ModelAndView getData(@RequestBody DataGrid dataGrid) {   }

但我收到此HttpMediaTypeNotSupportedException: Content type 'application/json' not supported错误。我很好奇它可能是因为我的formbean对象中的HsshMap DataGrid这就是这个对象的样子:

public class DataGrid implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 5979434283059930104L;
private String                  transactionAction;
private String                  transactionDate;
private List<GridColumn> columns;
private List<Map<GridColumnName,Object>> rows;


public Grid(){
    this.columns = new ArrayList<GridColumn>();
    this.rows = new ArrayList<Map<GridColumnName,Object>>();

}

    ...getter setter here

2 个答案:

答案 0 :(得分:0)

除了返回ResponseEntity之外,我遇到了完全相同的问题。我尝试了很多解决方案,但没有一个有效。添加了Jackson ASL依赖项和<mvc:annotationDriven />,但没有解决我的问题(虽然<mvc:annotationDriven />是强制性的)。然后找到以下链接:

  

https://spring.io/guides/gs/consuming-rest/

在pom.xml中添加以下依赖项重新解决了我的问题,这真是太棒了!

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.3.2</version>
</dependency>

答案 1 :(得分:0)

在调用Java方法时,在JS中编写以下代码:

var xhttp = new XMLHttpRequest();
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
相关问题