我正在关注websystique Spring 4 MVC tutorial,我正在尝试找出如何发送http
请求以匹配创建用户method
:
//-------------------Create a User--------------------------------------------------------
@RequestMapping(value = "/user/", method = RequestMethod.POST)
public ResponseEntity<Void> createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
System.out.println("Creating User " + user.getName());
if (userService.isUserExist(user)) {
System.out.println("A User with name " + user.getName() + " already exist");
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
userService.saveUser(user);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
或者一般来说,如何通过@RequestBody
发送与我的用户对象(POJO
)匹配的对象:
public class User {
private String m_name;
private long m_id;
private int m_age;
private int m_salary;
public User(String name, long id, int age, int salary){
this.m_name = name;
this.m_id = id;
this.m_age = age;
this.m_salary = salary;
}
//getters, setters...
}