我收到以下異常,不知道爲什麼......
異常線程「main」 組織。 springframework.web.client.RestClientException:無法提取 響應:沒有找到合適的HttpMessageConverter用於響應類型 [class com.avada.rest.UsersController $ Users]和內容類型 [application/json; charset = UTF-8] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:576) 在 org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529) 在 org.springframework.web.client .RestTemplate.getForObject(RestTemplate.java:236) 在com.avada.rest.UsersTest.main(UsersTest.java:18)
這是我RestController:
@RestController
@RequestMapping("/users")
public class UsersController {
@RequestMapping(method = RequestMethod.GET)
public Users getUsers() {
Users users = new Users();
users.setUsers(ConstantsHome.userprofileMgr.getUsers(null));
return users;
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable String id) {
return ConstantsHome.userprofileMgr.getUserByUserId(id, true, true);
}
public static class Users {
private List<User> users = new ArrayList<>();
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
}
這是我的測試等級:
public class UsersTest {
private static RestTemplate template = new RestTemplate();
public static void main (String[] args) throws Exception {
// Get all users
String uri = "http://localhost:8080/IR360/rest/users";
UsersController.Users users = template.getForObject(uri, UsersController.Users.class);
System.out.println("Looping through users...");
for (User user : users.getUsers()) {
System.out.println("Name=" + user.getName());
}
// Get 1 user
uri = "http://localhost:8080/IR360/rest/users/admin";
User user = template.getForObject(uri, User.class);
System.out.println("Name for single user=" + user.getName());
}
}
如果我註釋掉「獲取所有用戶」的測試代碼,我可以得到單個用戶沒問題。
我在做什麼錯在這段代碼?
P.S. - 我能爲getUsers()
呼叫通過瀏覽器和JSON回來罰款,所以我知道getUsers()
工作...只是不能得到RestTemplate
工作
建設 –
期間MappingJackson2HttpMessageConverter'的'實例來轉換你的'RestTemplate'的列表添加以下所有轉換器都存在於我的'RestTemplate':0 = {ByteArrayHttpMessageConverter @ 1366} 1 = {StringHttpMessageConverter @ 1367 } 2 = {ResourceHttpMessageConverter @ 1368} 3 = {SourceHttpMessageConverter @ 1369} 4 = {AllEncompassingFormHttpMessageConverter @ 1370} 5 = {Jaxb2RootElementHttpMessageConverter @ 1371} 6 = {MappingJackson2HttpMessageConverter @ 1372} –
您可以發佈您'Users'類 – Jaiwo99