MVC应用程序不接受JSON

MVC应用程序不接受JSON

本文介绍了Spring MVC应用程序不接受JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚为什么无法将更新发布到我的控制器.我正在尝试通过Chrome插件提交JSON数据.最终,我将对请求使用angular.我对照其他stackoverflow文章进行了检查,似乎我已经了解了他们提出的所有建议.

I cannot figure out why I cannot post updates to my controller. I am trying to submit json data via a chrome addon. Eventually I will be using angular for the requests. I checked against other stackoverflow articles and it seems I have everything that they suggest.

出于什么价值,我向没有问题的同一控制器发出了GET请求.

For what its worth I have a GET request to the same controller that is working without issue.

HTTP Status 415 -  The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

我的服务器日志显示以下内容

My server log shows the following

INFO - Mapped "{[/service/products/addProduct],methods=[POST],params=[],headers=[],consumes=[application/json],produces=[],custom=[]}" onto public void com.cr.controllers.ProductsController.addProduct(com.cr.entity.Products)

发布到地址

http://localhost:8082/service/products/addProduct

正在发布的数据

{
    "productId": 2,
    "productModel": "Product Model 2",
    "productName": "Product Name 2",
    "dateAdded": 1361880001000,
    "productWeight": 2,
    "productStatus": "Hidden",
    "productTaxClass": {
        "taxId": 2,
        "taxClassTitle": "High Tax Class",
        "taxClassDescription": "This is a high tax class",
        "lastModified": 1361880001000,
        "dateAdded": 1361880001000
    },
    "productImages": {
        "imageId": 2,
        "imageDescription": "Product Image 2",
        "imageTitle": "Image 2",
        "imagePath": "prd_02.jpg",
        "imageRelation": 1
    },
    "productManufacturer": {
        "manufacturerId": 2,
        "manufacturerName": "Factory 2",
        "manufacturerImage": null
    },
    "quantityAvailable": 4,
    "quantityInWarehouse": 4,
    "stockAlert": 1,
    "productCost": 1,
    "productRetail": 1,
    "productPrice": 1,
    "productSalePrice": 1,
    "saleInd": null,
    "productSku": null,
    "backOrderMessage": null,
    "inStockMessage": null,
    "outOfStockMessage": null,
    "manufacturerproductsku": null,
    "productDescriptionId": {
        "productTextId": 2,
        "productTextData": "Este es en espanol",
        "lastModified": 1361793601000
    }
}

控制器映射

@RequestMapping(value = "/service/products/addProduct",
        consumes = "application/json",
        method= RequestMethod.POST)
public @ResponseBody void addProduct(@RequestBody Products products){
    productsDao.createProduct(products);
}

web.xml

   <servlet-mapping>
        <servlet-name>cr</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>httpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>httpMethodFilter</filter-name>
        <servlet-name>cr</servlet-name>
    </filter-mapping>

_ UPDATE _ _

_ UPDATE __

我开始使用amplify来执行请求,因为我想确保它不是chrome插件.我现在要400.以下是我的服务器上显示的错误.

I started using amplify to do my requests because I wanted to be sure that it was not the chrome addon. I am getting a 400 now. Below is the error showing on my server.

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of com.cr.entity.Products out of START_ARRAY token
 at [Source: org.apache.catalina.connector.CoyoteInputStream@28d528d5; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.cr.entity.Products out of START_ARRAY token
 at [Source: org.apache.catalina.connector.CoyoteInputStream@28d528d5; line: 1, column: 1

这是扩增的定义和要求.

Here is the amplify definition and request.

 amplify.request.define("addRequest", "ajax", {
            url: "service/products/addProduct",
            type: "POST",
            dataType: 'json',
            contentType: 'application/json'
        });
        amplify.request({
            resourceId: "addRequest",
            data: JSON.stringify(jsonData),
            success: function () {
                alert("success")
            },
            error: function () {
                alert("fail")
            }
        });

数据:

var jsonData = [{
    "productId": 4,
    "productModel": "Product Model 2",
    "productName": "Product Name 2",
    "dateAdded": 1361880001000,
    "productWeight": 2,
    "productStatus": "Hidden",
    "productTaxClass": {
        "taxId": 2,
        "taxClassTitle": "High Tax Class",
        "taxClassDescription": "This is a high tax class",
        "lastModified": 1361880001000,
        "dateAdded": 1361880001000
    }
}];

推荐答案

我需要将以下内容添加到jsonConverter bean中.

I needed to add the following to the jsonConverter bean.

<property name="prefixJson" value="false"/>

最终豆如下

 <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="prefixJson" value="false"/>
        <property name="supportedMediaTypes" value="application/json"/>
    </bean>

另一篇堆栈溢出文章

这篇关于Spring MVC应用程序不接受JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 18:15