Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;     } } 
like image 495
POL Avatar asked Jul 20 '12 12:07

POL


People also ask

How do you set the header parameter in RestTemplate?

I suggest using one of the exchange methods that accepts an HttpEntity for which you can also set the HttpHeaders . (You can also specify the HTTP method you want to use.) For example, RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.

How do you pass the header in RestTemplate getForObject?

The RestTemplate getForObject() method does not support setting headers. The solution is to use the exchange() method. HttpHeaders headers = new HttpHeaders(); headers. set("Header", "value"); headers.

Is RestTemplate getting deprecated?

RestTemplate provides a synchronous way of consuming Rest services, which means it will block the thread until it receives a response. RestTemplate is deprecated since Spring 5 which means it's not really that future proof.


1 Answers

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.

like image 115
MattR Avatar answered Oct 08 '22 06:10

MattR