我是Rest的新手。我在网上用例子写了一个简单的程序。
然而,当我使用soapui(rest)来调用时,我得到错误415
{
"timestamp": 1474998766661,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Unsupported Media Type",
"path": "/user"
}
@RestController
public class testController {
@RequestMapping(value = "/user", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody User createUser(@RequestBody @Valid final User user) {
System.out.println("firstname: " + user.getFirstname());
System.out.println("lastname: " + user.getLastname());
return new User();
}
}
public class User {
private String firstname;
private String lastname;
public User() {
}
public User(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
答案 0 :(得分:1)
将请求Content-Type设置为“application / json”。