我很可能错过了一些简单的东西,或者不明白我想要做什么。带有JSON对象的Spring MVC POST
我有一个RESTful界面,我试图做一个帖子。使用POSTMAN,以下JSON可以正常工作。
{
"username": "uname",
"password": "pass",
"role": "role"
}
我的控制器看起来像
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<AccountResource> createAccount(@RequestBody AccountResource sentAccount) {
try {
Account createdAccount = accountService.createAccount(sentAccount.toAccount());
AccountResource res = new AccountResourceAssembler().toResource(createdAccount);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(URI.create(res.getLink("self").getHref()));
return new ResponseEntity<AccountResource>(res, headers, HttpStatus.CREATED);
}
catch (AccountExistsException exception) {
throw new ConflictException(exception);
}
}
但是当我尝试使用复合JSON对象
{
"username": "uname",
"password": "pass",
"role": "role",
"phones": {
"phone": {
"areacode": "303",
"prefix": "555",
"body": "6666",
"ext": "12345"
}
}
}
我甚至不得到控制,我得到一个错误。 ...
The request sent by the client was syntactically incorrect.
public class AccountResource extends ResourceSupport {
private String username;
private String password;
private String fname;
private String lname;
private String role;
private List<Phone> phones = new ArrayList<Phone>();
public String getUsername() {
return username;
}
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;
}
public void setUsername(String username) {
this.username = username;
}
@JsonIgnore
public String getPassword() {
return password;
}
@JsonProperty
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public List<Phone> getPhones() {
return phones;
}
public void setPhones(List<Phone> phones) {
this.phones = phones;
}
public Account toAccount() {
Account account = new Account();
account.setUsername(username);
account.setFname(fname);
account.setLname(lname);
account.setPassword(password);
account.setRole(role);
account.setPhones(phones);
return account;
}
}
@Entity
@Table(name = "phone")
@NamedQueries({
@NamedQuery(name = "Phone.findPhonesByAreaCode", query = "Select p from Phone p where p.areaCode=:areaCode")
})
public class Phone {
@Id
@GeneratedValue
private Long id;
private String areaCode;
private String prefix;
private String body;
private String ext;
private String type;
@ManyToOne
private Account account;
public Phone(String areaCode, String prefix, String body, String ext, String type) {
this.areaCode = areaCode;
this.prefix = prefix;
this.body = body;
this.ext = ext;
this.type = type;
}
public Phone(String areaCode, String prefix, String body, String type) {
this.areaCode = areaCode;
this.prefix = prefix;
this.body = body;
this.type = type;
}
public Phone() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAreaCode() {
return areaCode;
}
public void setAreaCode(String areaCode) {
this.areaCode = areaCode;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getExt() {
return ext;
}
public void setExt(String ext) {
this.ext = ext;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
有人能指出我在正确的方向吗?
谢谢...但不是问题的答案要么。它验证,像我的版本一样,但仍然得到错误。我认为这个问题是我的HATEOAS实现。时间倒退10和平底船。 – mortsahl 2014-12-07 18:27:35