我在 org.springframework.web.client.RestClientException 中收到以下错误:使用 rest 模板时 java.util.HashMap 没有 HttpMessageConverter。有谁知道出了什么问题?
使用 java 1.8、spring 5.2.2、commons-logging 1.2。
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.client.RestTemplate;
public class testapi {
private static final String API_BASE_URL = "https://xyz/rest/ng";
private static RestTemplate template = new RestTemplate();
public static void main(String[] args)
throws Exception {
String token = login();
}
private static String login() {
Map<String, Object> payload = new HashMap<>();
payload.put("loginName", "abc.com");
payload.put("password", "xyz");
Map<String, Object> resp = template.postForObject(getUrl("/sessions"), payload, Map.class);
return (String) resp.get("token");
}
}
错误
Exception in thread "main" org.springframework.web.client.RestClientException: No HttpMessageConverter for java.util.HashMap
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:964)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:677)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:421)
at TestFormApis.login(testapis.java:117)
at TestFormApis.main(testapis.java:61)