I am newbie in Spring. I generate the JSON like below:
[
{
"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:
$.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['']]
How to map my JSON to controller? Any help will be greatly appreciated!!!
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("/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";
}
}
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.
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:
<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:
$.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:
@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.
You need to define method=post
. also I added produces = "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";
}
在我的ajax请求中,我添加了contentType:application / json.After添加此控制器映射了我的ajax请求。谢谢所有。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.