您将名称和密码作为 uri 变量传递:
public <T> T postForObject(java.lang.String url,
@Nullable
java.lang.Object request,
java.lang.Class<T> responseType,
java.util.Map<java.lang.String,?> uriVariables)
throws RestClientException
docs.spring.io
如果您有一些网址,例如:
http://yourhost:8080/dosomethingwithemployee/name/password并且您从 url 本身提取了名称和密码,那么它可能会起作用。
String url = "http://yourhost:8080/dosomethingwithemployee/{name}/{password}"
restTemplate.postForObject(url, request, Employee.class, map);
但是,我认为您一直在尝试在请求正文中发送名称和密码:
public SomeType getResponse(String login, String password) {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
Employee employee = new Employee();
employee.setName(login);
employee.setPassword(password);
SomeType responseBody = post("http://locahost:8080/dosomethingwithemployee", employee, headers, SomeType.class);
return responseBody;
}
public <T> T post(String url, Object requestObject, MultiValueMap<String, String> headers, Class<T> responseType) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
HttpEntity request = new HttpEntity(requestObject, headers);
T responseObject = restTemplate.postForObject(url, request, responseType);
return responseObject;
}