使用Spring MVC的REST webservice在发布JSON时返回null
使用接受JSON请求主体的Spring MVC开发REST Web服务。 并进一步处理收到的消息。 Iam使用以下内容:Eclipse,Tomcat,Spring 3.0.1,Jackson lib,Curl用于测试Web服务
`curl -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"fname":"my_firstname" , "lname":"my_lastname"}' http://localhost:8080/SpringMVC/restful`
回国
"Saved person: null null"
我的控制器类
import com.samples.spring.Person;
@Controller
public class RestController {
@RequestMapping(value="{person}", method = RequestMethod.POST)
@ResponseBody
public String savePerson(Person person) {
// save person in database
return "Saved person: " + person.getFname() +" "+ person.getLname();
}
我的人课
package com.samples.spring;
public class Person {
public String fname;
public String lname;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
}
尝试添加@RequestBody
@RequestMapping(value="{person}", method = RequestMethod.POST)
@ResponseBody
public String savePerson(@RequestBody Person person) {
// save person in database
return "Saved person: " + person.getFname() +" "+ person.getLname();
}
尝试添加一个构造函数Person类:
public Person(String fname, String lname) {
this.fname = fname;
this.lname = lname;
}
链接地址: http://www.djcxy.com/p/48519.html
上一篇: REST webservice using Spring MVC returning null while posting JSON
下一篇: How to create a POST request in REST to accept a JSON input?