2
我想執行POST的JSON消息並想要轉換爲Employee Object。 JSON消息是{"employee":{"id":2231,"name":"jeffarry2231","niNumber":"SN10KTL"}}
。默認的JSON到對象映射不起作用
Employee對象
public class Employee {
private Long id;
private String name;
private String niNumber;
...
}
的EmployeeController
@Controller
public class EmployeeController {
@RequestMapping(value = "/employee/add/", method = RequestMethod.POST)
public void addEmployee(Employee employee){
System.out.println(employee.getName());
}
}
的RestTemplate被過帳請求
@Test
public void postMethod() {
RestTemplate restTemplate = new RestTemplate();
String jsonEmployee = "{'id':2231,'name':'jeffarry2231','niNumber':'SN10KTL'}}";
HttpHeaders headers = new HttpHeaders();
headers.setAccept(newArrayList(MediaType.APPLICATION_JSON));
HttpEntity<String> requestEntity = new HttpEntity<String>(jsonEmployee, headers);
restTemplate.exchange("http://localhost:8080/employee/add/", POST, requestEntity, String.class);
}
applicationContext.xml文件具有
<mvc:annotation-driven/>
我期待MappingJacksonHttpMessageConverter默認使用,但它似乎沒有轉換,不知道我在這裏錯過了什麼!
感謝!這就是我失蹤了:-) – tintin