Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting request header content-type to json in Spring Framework resttemplate [duplicate]

I'm learning Spring Framework to create a client of a REST web service that uses basic authentication and exchanges JSON. After much searching on the web, I wrote some code that worked (below), but now I'm getting an "Unsupported Media Type" error because the requests are sent with Content-Type text/plain rather than application/json. I've found nothing on the web that shows how to set Content-Type in the request header (without getting completely lost in the weeds). My code is:

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

...

BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("login", "password"));
HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

RestTemplate restTemplate = new RestTemplate(requestFactory);
String url = "http://host:8080/path/";
String postBody = getPostInput("filename");
jsonString = restTemplate.postForObject(path, postBody, String.class);

Any guidance would be greatly appreciated.

Thanks, George

like image 522
user2752012 Avatar asked Apr 02 '15 16:04

user2752012


People also ask

How do I add content type to RestTemplate?

To achieve this, we'll add a Content-Type header to our request with the APPLICATION_JSON media type. Spring's HttpHeaders class provides different methods to access the headers. Here, we set the Content-Type header to application/json by calling the setContentType method.

How do you set a spring RestTemplate header?

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 I POST data from JSON to RestTemplate?

add(new MappingJacksonHttpMessageConverter()); restTemplate. setMessageConverters(list); ... Payment payment= new Payment("Aa4bhs"); Payment res = restTemplate. postForObject("http://localhost:8080/aurest/rest/payment", payment, Payment.


1 Answers

you can try using any method from below code

1

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> entity = new HttpEntity<String>(postBodyJson ,headers);
restTemplate.put(uRL, entity);

2

RequestEntity<String> requestEntity = RequestEntity .post(new URL(attributeLookupUrl).toURI()) .contentType(MediaType.APPLICATION_JSON) .body(postBodyJson); 
restTemplate.exchange(requestEntity, responseClass);

3

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

// if you need to pass form parameters in request with headers.
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("username", userName);
map.add("password", password);

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<TokenVO> responses = restTemplate.postForEntity(URL, request, responseClass);
like image 126
ManojP Avatar answered Oct 10 '22 05:10

ManojP