Spring RestTemplate postForObject with Header: webservice can't find my header parameters

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

I'm struggling with RestTemplate. I need to POST some authentication information to a rest webservice. I can send a request and I get a response. But according to the response my header parameters are not getting through. (Sending the same request with SOAPUI works fine)

This is my code:

HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); headers.add("companyId", companyId); headers.add("password", password);                HttpEntity<String> request = new HttpEntity<String>(headers);  RestTemplate restTemplate = new RestTemplate();  List<HttpMessageConverter<?>> messageConverters = new  ArrayList<HttpMessageConverter<?>>(); messageConverters.add(new MappingJacksonHttpMessageConverter());            restTemplate.setMessageConverters(messageConverters);  LoginResponse response = (LoginResponse)restTemplate.postForObject(url, request, LoginResponse.class); 

Anyone who can tell me what's wrong with my HttpEntity or HttpHeader?

thank you.

SOLVED:

Ok, finally got it working.

MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.add("companyId", companyId); map.add("password", password);    HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);  List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); messageConverters.add(new MappingJacksonHttpMessageConverter());     messageConverters.add(new FormHttpMessageConverter()); restTemplate.setMessageConverters(messageConverters);  LoginResponse response = (LoginResponse) restTemplate.postForObject(url, request, LoginResponse.class);  

Because I also had a hard time on the response, maybe it can be useful to others:

@JsonIgnoreProperties(ignoreUnknown = true) public class ItcLoginResponse {      public String loginToken;      @JsonProperty("token")     public String getLoginToken() {         return loginToken;     }      public void setLoginToken(String loginToken) {         this.loginToken = loginToken;     } } 

回答1:

You're setting a header indicating you're posting a form (APPLICATION_FORM_URLENCODED), but then setting companyId and password as HTTP headers.

I suspect you want those fields to be in the body of your request.

You're also creating an HttpEntity<String> which indicates you're going to post a request body containing a String, but you're providing headers but no String.

If that doesn't help you fix it perhaps you can explain what the request is supposed to look like.



回答2:

You can set Header value as Authorization

// Set the username and password for creating a Basic Auth request

HttpAuthentication authHeader = new HttpBasicAuthentication(username, password); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAuthorization(authHeader); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);  // Create a new RestTemplate instance RestTemplate restTemplate = new RestTemplate();  // Add the String message converter restTemplate.getMessageConverters().add(new StringHttpMessageConverter());  try {      // Make the HTTP GET request to the Basic Auth protected URL     ResponseEntity<Message> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);      return response.getBody();  } catch (HttpClientErrorException e) {      Log.e(TAG, e.getLocalizedMessage(), e);     // Handle 401 Unauthorized response } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!