gpt4 book ai didi

Spring MVC 应用程序不接受 JSON

转载 作者:行者123 更新时间:2023-12-04 15:13:06 27 4
gpt4 key购买 nike

我不知道为什么我不能向我的 Controller 发布更新。我正在尝试通过 chrome 插件提交 json 数据。最终我将使用 angular 来处理请求。我检查了其他 stackoverflow 文章,似乎我有他们建议的一切。

对于它的值(value),我有一个对同一个 Controller 的 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
}
}

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

网页.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>

_ 更新 __

我开始使用 amplify 来处理我的请求,因为我想确定它不是 chrome 插件。我现在要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.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。

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

Another Stack Overflow Article

关于Spring MVC 应用程序不接受 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15145460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com