Я, скорее всего, пропустил что-то простое или не понимаю, что я пытаюсь сделать.Spring MVC POST с объектом JSON
У меня есть интерфейс 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. Время отступать назад и пунт. – mortsahl