2014-12-07 76 views
0

我很可能錯過了一些簡單的東西,或者不明白我想要做什麼。帶有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; 
} 

有人能指出我在正確的方向嗎?

+0

謝謝...但不是問題的答案要麼。它驗證,像我的版本一樣,但仍然得到錯誤。我認爲這個問題是我的HATEOAS實現。時間倒退10和平底船。 – mortsahl 2014-12-07 18:27:35

回答

0

在這種情況下,很容易寫一個JUnit和使用傑克遜ObjectMapper(它應該已經在你的類路徑)。

import com.fasterxml.jackson.core.JsonProcessingException; 
import com.fasterxml.jackson.databind.ObjectMapper; 

public class TestAcountResource { 

    protected ObjectMapper mapper = new ObjectMapper(); 

    @Test 
    public void test() throws JsonProcessingException { 
     Phone phone1 = new Phone(); 
     phone1.setAreaCode("303"); 
     phone1.setPrefix("555"); 
     phone1.setBody("6666"); 
     phone1.setExt("12345"); 
     Phone phone2 = new Phone(); 
     phone2.setAreaCode("304"); 
     phone2.setPrefix("556"); 
     phone2.setBody("6667"); 
     phone2.setExt("12346"); 
     List<Phone> phones = new ArrayList<>(); 
     phones.add(phone2); 
     phones.add(phone1); 
     AccountResource ar = new AccountResource(); 
     ar.setFname("fname"); 
     ar.setLname("lname"); 
     ar.setPassword("password"); 
     ar.setUsername("username"); 
     ar.setRole("role"); 
     ar.setPhones(phones); 

     String accountAsJson = mapper.writeValueAsString(ar.toAccount()); 

     System.out.print(accountAsJson); 

    } 

} 

因此,基於模仿你的類就應該是類似下面的文字:

{ 
    "username" : "username", 
    "fname" : "fname", 
    "lname" : "lname", 
    "password" : "password", 
    "role" : "role", 
    "phones" : [{ 
      "id" : null, 
      "areaCode" : "304", 
      "prefix" : "556", 
      "body" : "6667", 
      "ext" : "12346", 
      "type" : null 
     }, { 
      "id" : null, 
      "areaCode" : "303", 
      "prefix" : "555", 
      "body" : "6666", 
      "ext" : "12345", 
      "type" : null 
     } 
    ] 
} 
+0

謝謝!這就是答案!我需要更多的測試 - 並且應該更多地成爲TDD的粉絲。 – mortsahl 2014-12-07 20:07:18

1

我想你的JSON應該是這樣的 -

{ 
    "username": "uname", 
    "password": "pass", 
    "role": "role", 
    "phones": [ 
     "phone": { 
      "areacode": "303", 
      "prefix": "555", 
      "body": "6666", 
      "ext": "12345" 
     } 
    ] 
} 
+0

向我們展示AccountResource類和電話類 – 2014-12-07 06:58:12

+0

謝謝 - 這沒有幫助,同樣不好的語法錯誤。我已經在各處設置了斷點,並且如果它到達那裏,還沒有找到它的代碼。我已將我的AccountResource和Phones類添加到原始問題中。 – mortsahl 2014-12-07 16:07:17

+0

順便說一句,這是正確的語法......我有另一個問題,使我相信這是不正確的。 Thx – mortsahl 2014-12-08 23:50:16

0

我想你應該嘗試使用手機而不是List的數組中的AccountResource中豆

電話[]手機;

並且json應該像

{ 
"username": "uname", 
"password": "pass", 
"role": "role", 
"phones": { 
    { 
     "areacode": "303", 
     "prefix": "555", 
     "body": "6666", 
     "ext": "12345" 
    } 
} 

}