Spring MVC的应用程序不接受JSON(Spring MVC application not

2019-08-17 05:33发布

我想不通,为什么我不能发布更新我的控制器。 我想通过一个插件铬提交JSON数据。 最后,我将是采用了棱角分明的请求。 我检查了与其他计算器文章,看来我,他们认为一切。

对于什么它的价值我有一个GET请求与工作没有问题,相同的控制器。

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.  

我的服务器日志显示了以下

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 _ _ _

我开始使用放大做我的请求,因为我想确保它不是镀铬插件。 现在我得到一个400。 下面是显示我的服务器上的错误。

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

下面是定义AMPLIFY和请求。

 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
    }
}];

Answer 1:

我需要将以下内容添加到jsonConverter豆。

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

最后一个bean是如下

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

另一个堆栈溢出文章



文章来源: Spring MVC application not accepting JSON