JSON再加上Spring MVC的3.2错误415(不支持的媒体类型) [英] JSON plus spring mvc 3.2 error 415 (Unsupported Media Type)

查看:175
本文介绍了JSON再加上Spring MVC的3.2错误415(不支持的媒体类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了什么错?我尝试使用Spring MVC和JSON。当我尝试调试我的code我找了JavaScript的工作,但是没有工作控制器。在浏览器中,我得到错误415不支持的媒体类型。

脚本:

  $(文件)。就绪(函数(){
  $('#newSmartphoneForm)。递交(函数(事件){

      。VAR制片= $('#制片人)VAL();
      。VAR模型= $('#模式)VAL();
      VAR价格= $('#价)VAL()。
      VAR JSON = {制片人:制片人,模式:模式,价格:价格};

    $阿贾克斯({
        网址:$(#newSmartphoneForm)ATTR(行动),
        数据:JSON.stringify(JSON)
        键入:POST,

        beforeSend:功能(XHR){
            xhr.setRequestHeader(接受,应用/ JSON);
            xhr.setRequestHeader(内容类型,应用/ JSON);
        },
        成功:函数(智能手机){
            VAR respContent =;

            respContent + =<跨度类='成功'>智能手机被创造:[;
            respContent + = smartphone.producer +:
            respContent + = smartphone.model +:
            respContent + = smartphone.price +]< / SPAN>中;

            $(#sPhoneFromResponse)HTML(respContent);
        }
    });

    。事件preventDefault();
  });

});
 

控制器:

  @RequestMapping(值=/制造,方法= RequestMethod.POST,
        生产= MediaType.APPLICATION_JSON_VALUE,
            消耗= MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
公共智能手机createSmartphone(@RequestBody智能手机的智能手机){
    返回smartphoneService.create(智能手机);
}
 

解决方案

这可能会发生,因为你没有杰克逊在classpath在运行时。

该错误消息说,服务器无法处理由于某种原因,你的JSON请求。 JSON转换为Java对象与一个称为的事消息变换器的。如果你有< MVC:注解驱动/> 在Spring XML配置(或者你有Java的配置允许),JSON消息转换器自动注册。如果你不这样做,你必须注册。

What did I do wrong? I try to use Spring mvc and JSON. When I try to debug my code I am looking that javascript works but doesn't works controller. In browser I get error 415 Unsupported Media Type.

Script:

$(document).ready(function() {
  $('#newSmartphoneForm').submit(function(event) {

      var producer = $('#producer').val();
      var model = $('#model').val();
      var price = $('#price').val();
      var json = { "producer" : producer, "model" : model, "price": price};

    $.ajax({
        url: $("#newSmartphoneForm").attr( "action"),
        data: JSON.stringify(json),
        type: "POST",

        beforeSend: function(xhr) {
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-Type", "application/json");
        },
        success: function(smartphone) {
            var respContent = "";

            respContent += "<span class='success'>Smartphone was    created: [";
            respContent += smartphone.producer + " : ";
            respContent += smartphone.model + " : " ;
            respContent += smartphone.price + "]</span>";

            $("#sPhoneFromResponse").html(respContent);         
        }
    });

    event.preventDefault();
  });

});  

Controllers:

   @RequestMapping(value="/create", method=RequestMethod.POST, 
        produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Smartphone createSmartphone(@RequestBody Smartphone smartphone) {
    return smartphoneService.create(smartphone);
}

解决方案

It may be happening because you do not have Jackson on your classpath at runtime.

The error message says that the server cannot handle your JSON request for some reason. JSON is converted to a Java object with a thing that is called message converter. If you have <mvc:annotation-driven /> in your Spring XML config (or you have Java Config enabled), the JSON message converter is registered automatically. If you don't, you have to register it.

这篇关于JSON再加上Spring MVC的3.2错误415(不支持的媒体类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆