I am newbie in Spring. I generate the JSON like below:
我是春天的新手。我生成如下的JSON:
[
{
"customer" : "16", "project" : "19",
"phase" : "47", "approver" : "5",
"date1" : "", "date2" : "",
"date3" : "", "date4" : "",
"date5" : "", "date6" : "",
"date7" : "", "activity" : "1"
},
{
"customer" : "17", "project" : "20",
"phase" : "48", "approver" : "3",
"date1" : "", "date2" : "",
"date3" : "", "date4" : "",
"date5" : "", "date6" : "",
"date7" : "", "activity" : "1"
}
]
I am passed this JSON to my Spring controller:
我将此JSON传递给我的Spring控制器:
$.ajax({
type: 'post',
url: 'NewTimesheet',
dataType : 'json',
data: JSON.stringify(jsonObj),
success: function(data) {
console.log(data);
}
});
I am mapped the request to controller like below:
我将请求映射到控制器,如下所示:
@RequestMapping(value="NewTimesheet", headers = { "Content-type=application/json" })
@ResponseBody
public String addNewTimesheet(@RequestBody List<Timesheet> timesheet,
HttpSession session) {
logger.info("timesheet list size is" + timesheet.size());
return "success";
}
Timesheet
class:
时间表类:
public class Timesheet {
private String project;
private String phase;
private String approver;
private String customer;
private String date1;
private String date2;
private String date3;
private String date4;
private String date5;
private String date6;
private String date7;
private String activity;
//Getters and setters
}
But my request is not mapped with the conroller. My console displays like below:
但我的请求未与控制器映射。我的控制台显示如下:
WARN org.springframework.web.servlet.PageNotFound.handleNoSuchRequestHandlingMethod:142 - No matching handler method found for servlet request: path '/NewTimesheet', method 'POST', parameters map['[{"customer":"16","project":"19","phase":"47","approver":"5","date1":"","date2":"","date3":"","date4":"","date5":"","date6":"","date7":"","activity":"1"},{"customer":"17","project":"20","phase":"48","approver":"3","date1":"","date2":"","date3":"","date4":"","date5":"","date6":"","date7":"","activity":"1"}]' -> array['']]
WARN org.springframework.web.servlet.PageNotFound.handleNoSuchRequestHandlingMethod:142 - 找不到servlet请求的匹配处理程序方法:path'/ NewTimesheet',方法'POST',参数map ['[{“customer”:“16”,“项目 “:” 19" , “相”: “47”, “批准”: “5”, “日期1”: “”, “日期2”: “”, “DATE3”: “”, “date4”: “” “date5”: “”, “date6”: “”, “date7”: “”, “活性”: “1”},{ “顾客”: “17”, “项目”: “20”,“相“:” 48" , “批准”: “3”, “日期1”: “”, “日期2”: “”, “DATE3”: “”, “date4”: “”, “date5”: “”,” date6“:”“,”date7“:”“,”activity“:”1“}]' - > array ['']]
How to map my JSON to controller? Any help will be greatly appreciated!!!
如何将我的JSON映射到控制器?任何帮助将不胜感激!!!
5 个解决方案
#1
3
You need to annotate the class as Controller, add a RequestMapping in your class and the HTTP method your calling in your method.
您需要将类注释为Controller,在类中添加RequestMapping,在方法中添加调用的HTTP方法。
@Controller
@RequestMapping("/NewTimesheet")
public class MyClassName {
@RequestMapping(value={ "", "/" }, method = RequestMethod.POST, headers = { "Content-type=application/json" })
@ResponseBody
public String addNewTimesheet(@RequestBody List<Timesheet> timesheet,HttpSession session){
logger.info("timesheet list size is"+timesheet.size());
return "success";
}
}
#2
2
Change @RequestBody to @ModelAttribute before the list in the controller. And in your json, put 'timesheet.' before every field, that is, timesheet.customer,timesheet.project.... such like that. That would work.
在控制器中的列表之前将@RequestBody更改为@ModelAttribute。在你的json中,放上'时间表'。在每个字段之前,即timesheet.customer,timesheet.project ....就像那样。那会有用。
#3
2
You need to define method=post
. also I added produces = "application/json"
你需要定义method = post。我还添加了produce =“application / json”
@RequestMapping(value="NewTimesheet", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public String addNewTimesheet(@RequestBody List<Timesheet> timesheet,HttpSession session){
logger.info("timesheet list size is"+timesheet.size());
return "success";
}
#4
2
A couple things that might be causing problems for you:
可能会给您带来问题的一些事情:
-
Make sure you have all of the necessary dependencies for Jackson. For Maven, this would be:
确保你拥有Jackson的所有必要依赖项。对于Maven,这将是:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.2.2</version> </dependency>
-
You don't need to stringify your JavaScript object, this is done implicitly. You should also name your submitted variable, since it must map to the domain object:
您不需要对JavaScript对象进行字符串化,这是隐式完成的。您还应该为提交的变量命名,因为它必须映射到域对象:
$.ajax({ method : 'post', url : 'NewTimesheet', dataType : 'json', data:{ 'timesheet': jsonObj }, success : function(data) { console.log(data); } });
-
Your controller should be configured to explicitly handle a POST request. Setting the accepted content type in the headers is not necessary. Also, I believe you need to map your domain objects to an array and not a list:
您的控制器应配置为显式处理POST请求。不需要在标题中设置接受的内容类型。另外,我认为您需要将域对象映射到数组而不是列表:
@RequestMapping(value="NewTimesheet", method = RequestMethod.POST) public @ResponseBody String addNewTimesheet(@RequestParam("timesheet") Timesheet[] timesheet,HttpSession session){ logger.info("timesheet list size is"+timesheet.length); return "success"; }
If you are using a relatively recent version of Spring MVC, there is no additional configuration required to handle requests for and producing JSON. Your AJAX request specifies a JSON response, which Spring will recognize and delegate to Jackson for serializing the input and output.
如果您使用的是相对较新版本的Spring MVC,则不需要其他配置来处理JSON的请求和生成JSON。您的AJAX请求指定了一个JSON响应,Spring将识别并委托给Jackson以序列化输入和输出。
#5
1
In my ajax request i added the contentType:application/json.After adding this controller mapped my ajax request.Thanks for all.
在我的ajax请求中,我添加了contentType:application / json.After添加此控制器映射了我的ajax请求。感谢所有。
#1
3
You need to annotate the class as Controller, add a RequestMapping in your class and the HTTP method your calling in your method.
您需要将类注释为Controller,在类中添加RequestMapping,在方法中添加调用的HTTP方法。
@Controller
@RequestMapping("/NewTimesheet")
public class MyClassName {
@RequestMapping(value={ "", "/" }, method = RequestMethod.POST, headers = { "Content-type=application/json" })
@ResponseBody
public String addNewTimesheet(@RequestBody List<Timesheet> timesheet,HttpSession session){
logger.info("timesheet list size is"+timesheet.size());
return "success";
}
}
#2
2
Change @RequestBody to @ModelAttribute before the list in the controller. And in your json, put 'timesheet.' before every field, that is, timesheet.customer,timesheet.project.... such like that. That would work.
在控制器中的列表之前将@RequestBody更改为@ModelAttribute。在你的json中,放上'时间表'。在每个字段之前,即timesheet.customer,timesheet.project ....就像那样。那会有用。
#3
2
You need to define method=post
. also I added produces = "application/json"
你需要定义method = post。我还添加了produce =“application / json”
@RequestMapping(value="NewTimesheet", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public String addNewTimesheet(@RequestBody List<Timesheet> timesheet,HttpSession session){
logger.info("timesheet list size is"+timesheet.size());
return "success";
}
#4
2
A couple things that might be causing problems for you:
可能会给您带来问题的一些事情:
-
Make sure you have all of the necessary dependencies for Jackson. For Maven, this would be:
确保你拥有Jackson的所有必要依赖项。对于Maven,这将是:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.2.2</version> </dependency>
-
You don't need to stringify your JavaScript object, this is done implicitly. You should also name your submitted variable, since it must map to the domain object:
您不需要对JavaScript对象进行字符串化,这是隐式完成的。您还应该为提交的变量命名,因为它必须映射到域对象:
$.ajax({ method : 'post', url : 'NewTimesheet', dataType : 'json', data:{ 'timesheet': jsonObj }, success : function(data) { console.log(data); } });
-
Your controller should be configured to explicitly handle a POST request. Setting the accepted content type in the headers is not necessary. Also, I believe you need to map your domain objects to an array and not a list:
您的控制器应配置为显式处理POST请求。不需要在标题中设置接受的内容类型。另外,我认为您需要将域对象映射到数组而不是列表:
@RequestMapping(value="NewTimesheet", method = RequestMethod.POST) public @ResponseBody String addNewTimesheet(@RequestParam("timesheet") Timesheet[] timesheet,HttpSession session){ logger.info("timesheet list size is"+timesheet.length); return "success"; }
If you are using a relatively recent version of Spring MVC, there is no additional configuration required to handle requests for and producing JSON. Your AJAX request specifies a JSON response, which Spring will recognize and delegate to Jackson for serializing the input and output.
如果您使用的是相对较新版本的Spring MVC,则不需要其他配置来处理JSON的请求和生成JSON。您的AJAX请求指定了一个JSON响应,Spring将识别并委托给Jackson以序列化输入和输出。
#5
1
In my ajax request i added the contentType:application/json.After adding this controller mapped my ajax request.Thanks for all.
在我的ajax请求中,我添加了contentType:application / json.After添加此控制器映射了我的ajax请求。感谢所有。