【问题标题】:HttpMediaTypeNotSupportedException: Content type 'application/json' not supportedHttpMediaTypeNotSupportedException:不支持内容类型“应用程序/json”
【发布时间】:2014-05-02 15:42:34
【问题描述】:

您好,我正在使用 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

【问题讨论】:

  • 你的类路径中有 Jackson 吗? MappingJackson(2)HttpMessageConverter 可能因此未注册。
  • 如果你有 Jackson,哪个版本?

标签: spring-mvc jackson


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    除了返回ResponseEntity&lt;T&gt; 之外,我遇到了完全相同的问题。我尝试了很多解决方案,但都没有奏效。添加了 Jackson ASL 依赖项和 &lt;mvc:annotationDriven /&gt; 也没有解决我的问题(尽管 &lt;mvc:annotationDriven /&gt; 是强制性的)。然后找到下面的链接:

    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>
    

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 2021-06-11
      • 2018-09-05
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2022-12-22
      • 2018-11-06
      • 2015-06-21
      相关资源
      最近更新 更多