如何使用json发送一个使用JavaScript到Spring控制器的对象?

时间:2022-12-01 16:16:54

I am making simple Spring MVC web application. In my application I want to send a object from front end to Spring controller method. But when I do this I am getting js error

我正在制作简单的Spring MVC web应用程序。在我的应用程序中,我想将一个对象从前端发送到Spring控制器方法。但当我这样做时,我得到了js误差

500 (Internal Server Error)

I tried to find a good answer in internet but still could not find a good one.

我试图在网上找到一个好的答案,但还是找不到一个好的。

My controller method is

我的控制器方法

        @RequestMapping(value = "/add", method = RequestMethod.GET, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody String addInventory(@RequestBody InventoryAddParam inventoryAddParam) {

    log.info("addInventory");
    ServiceRequest<InventoryAddParam> serviceRequest =  convertAddParamtoServiceRequest(inventoryAddParam);
    validate(inventoryAddParam);
    inventoryService.addInventory(serviceRequest);
    return "Inventory Added Succesfully";
}                             

inventoryAddParam is a Serializable object that contain only String parameters.

inventoryAddParam是一个可序列化的对象,只包含字符串参数。

My JavaScript function to send object is

我的JavaScript函数发送对象是。

function sendDataTest() {
$.ajax({
    url : "/GradleSpringMVC/inventory/add",
    type : 'GET',
    dataType : 'json',
    data : JSON.stringify(populateTestObject()),
    contentType : 'application/json',
    mimeType : 'application/json'
}).done(function(data) {
    // temGrid.addJSONData(data);
}).fail(function(error) {
    // parseToPageAlerts(error.responseText);
}).always(function() {
    // hideLoading()
});}

I am creating the addParam object to send as ajax call. It create in function

我正在创建要作为ajax调用发送的addParam对象。它创建的函数

function populateTestObject(){
var addParam = new InventoryAddParam();
addParam.inventoryId = "INV001";
addParam.name = "ECG MACHINE";
addParam.price = "1000";
addParam.hospital = "Colombo";
addParam.userNote = "User Note";
return addParam;}      

Do I have done any wrong thing here ?

我做错什么了吗?

Other details about the error

关于错误的其他细节

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/GradleSpringMVC/inventory/add
Request Method:POST
Status Code:500 Internal Server Error

Header

Connection:close
Content-Language:en
Content-Length:4939
Content-Type:text/html;charset=utf-8
Date:Wed, 21 Jan 2015 03:55:19 GMT
Server:Apache-Coyote/1.1

2 个解决方案

#1


2  

You spring method consumes application/json and the error message says that what you're sending is not a valid JSON.

您的spring方法使用application/json,错误消息说您发送的不是有效的json。

Following that line, I believe that your issue is a $.extend(addParam), when set with a single argument it will extend a jQuery namespace with the object, and won't return any JSON object to be stringified (and nothing being sent to the server). The possible solution is either removing the extend function, or adding another parameter in which case a valid JSON object will be returned, e.g.

按照这一行,我认为您的问题是$.extend(addParam),当使用单个参数设置时,它将使用对象扩展jQuery名称空间,并且不会返回任何要被字符串化的JSON对象(并且不会向服务器发送任何内容)。可能的解决方案是删除扩展函数,或者添加另一个参数,在这种情况下返回一个有效的JSON对象,例如。

function sendData() {
var addParam = createAddParam();
$.ajax({
    url : "/GradleSpringMVC/inventory/add",
    type : 'GET',
    dataType : 'json',
    data : JSON.stringify(addParam),
    contentType : 'application/json',
    mimeType : 'application/json'
}).done(function(data) {
    // temGrid.addJSONData(data);
}).fail(function(error) {
    // parseToPageAlerts(error.responseText);
}).always(function() {
    // hideLoading()
});} 

#2


1  

Found the answer. Just changed the version of jackson to 1.9.12

找到了答案。只是把杰克逊的版本改成了1.9.12。

'org.codehaus.jackson:jackson-mapper-asl:1.9.12'

#1


2  

You spring method consumes application/json and the error message says that what you're sending is not a valid JSON.

您的spring方法使用application/json,错误消息说您发送的不是有效的json。

Following that line, I believe that your issue is a $.extend(addParam), when set with a single argument it will extend a jQuery namespace with the object, and won't return any JSON object to be stringified (and nothing being sent to the server). The possible solution is either removing the extend function, or adding another parameter in which case a valid JSON object will be returned, e.g.

按照这一行,我认为您的问题是$.extend(addParam),当使用单个参数设置时,它将使用对象扩展jQuery名称空间,并且不会返回任何要被字符串化的JSON对象(并且不会向服务器发送任何内容)。可能的解决方案是删除扩展函数,或者添加另一个参数,在这种情况下返回一个有效的JSON对象,例如。

function sendData() {
var addParam = createAddParam();
$.ajax({
    url : "/GradleSpringMVC/inventory/add",
    type : 'GET',
    dataType : 'json',
    data : JSON.stringify(addParam),
    contentType : 'application/json',
    mimeType : 'application/json'
}).done(function(data) {
    // temGrid.addJSONData(data);
}).fail(function(error) {
    // parseToPageAlerts(error.responseText);
}).always(function() {
    // hideLoading()
});} 

#2


1  

Found the answer. Just changed the version of jackson to 1.9.12

找到了答案。只是把杰克逊的版本改成了1.9.12。

'org.codehaus.jackson:jackson-mapper-asl:1.9.12'