I am not able to call Spring RestTemplate with HttpEntity for POST request. My call to RestTemplate gives Base64 string in Postman but using my java implementation it gives following error:
java.lang.IllegalArgumentException: Illegal base64 character 5b
at java.util.Base64$Decoder.decode0(Base64.java:714)
at java.util.Base64$Decoder.decode(Base64.java:526)
at java.util.Base64$Decoder.decode(Base64.java:549)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
My implementation is:
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
final HttpEntity<String> request = new HttpEntity<String>(searchRequestInput, headers);
final ResponseEntity<String> postForEntity = restTemplate
.postForEntity(baseURL, request, String.class);
String response = postForEntity.getBody();
I have tried following solutions, but it didn't work here
You can use HttpMessageConverter to request your restTemplate call for HttpEntity. Which can read and write Strings from the HTTP request and response.
From doc : By default, this converter supports all text media types (text/*), and writes with a Content-Type of text/plain.
You can try this by implementing StringHttpMessageConverter as below:
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
final HttpEntity<String> request = new HttpEntity<String>(searchRequestInput, headers);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new StringHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
then call restTemplate with your request.
Try these utility methods:
public static <T> ResponseEntity<T> makeRestRequest(Object entity, String restUrl, HttpMethod method, Class<T> entityClass) {
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = makeHttpEntity(entity);
ResponseEntity<T> response = null;
try {
response = restTemplate.exchange(restUrl, method, httpEntity, entityClass);
} catch (HttpClientErrorException e) {
e.printStackTrace();
return new ResponseEntity<>(e.getStatusCode());
}
return response;
}
public static <T> HttpEntity makeHttpEntity(T entity) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
HttpEntity<T> httpEntity = new HttpEntity<>(entity, headers);
return httpEntity;
}
Where in the these two method
- entity: your input object
- restUrl: your url
- HttpMethod: POST/GET
- entityClass: expected output object from the server
Please try with below code may it help you:
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
HttpEntity requestEntity;
if (entity instanceof String) {
requestEntity = new HttpEntity(headers);
} else {
requestEntity = new HttpEntity(searchRequestInput, headers);
}
try {
String response = restTemplate.exchange(baseURL, HttpMethod.POST, requestEntity, String.class).getBody().toString());
} catch (HttpServerErrorException | HttpClientErrorException e) {
e.printStackTrace();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.