hello everyone I have a problem that I do not understand why this is happening. I am working with spring boot and deploying with Kubernetes. after a while, the restTemplate stops working and I do not know what can be the cause. this is how I am creating the restTemplate class
#Bean
public RestTemplate vanillaRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new RestTemplateErrorHandler());
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
return restTemplate;
}
after a while some of my APIs that is using restTemplate returns an error like this:
{
"status": 500,
"body": "{ message: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.List] and content type [application/json;charset=UTF-8] , cause: null }",
}
this is only an example. resttemplate will have a problem with many classes such as Map, String, etc when this happens. and this is how I am sending the request:
ResponseEntity<List> adsRes = restTemplate.getForEntity(url, List.class);
the weird part is when I restart the app on the server everything starts working again and the APIs have no problem at all. even the ones that returned an error.
I have read this question but, as you can see I have not set any interceptor or anything like that to resttemplate
thanks in advance
Try replace the way how you are calling to the endpoint by the following code
ResponseEntity<YourObject[]> response = restTemplate.getForEntity("urlService",
YourObject[].class);
YourObject[] yourObjects= response.getBody();
List<YourObject> list = Arrays.asList(yourObjects);
Related
I am exposing an endpoint that accepts a Set<> as a #RequestBody this way :
public #ResponseBody ResponseEntity<Response> addTeamOwner(#RequestParam("teamName") String teamName, #RequestBody Set<String> emails, HttpServletRequest request){...}
And my Angular frontend is calling this endpoint like this :
let params = new HttpParams().set('teamName', teamName);
let url = `${UrlManager.TEAMS}/addOwners?${params.toString()}`;
this.httpClient.post<any>(url, emails);
For some reason I'm getting 400 Bad Request : HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: 'Bad Request', url: 'http://localhost:4200/api/teams/addOwners?teamName=DEMO_TEAM', ok: false, …}
It seems that the Set that Angular is sending is not accepted by the backend because when I change to an Array everything works fine !
FYI, my API is SpringBoot and my frontend is Angular.
Actually it is not possible to serialize data sent within Set because the data are not stored as properties.
The solution was to convert the set to an array this way :
this.httpClient.post<any>(url, [...emails]);
and the backend is able to deserialize it as a Set correctly.
WebSphere don't load previous classes (com.fasterxml.jackson.core.JsonGenerator and com.fasterxml.jackson.databind.ObjectMapper) of RestTemplate for do a REST call with customize class to catch JSON in response.
Now I'm trying to find an alternative to RestTemplate for do REST call.
Anyone have an idea?
This is my actually code but don't load previous classes so don't match JSON
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Person> restResponse = restTemplate.getForEntity(providerUrl, Person.class);
Did you consider the following:
ResponseEntity<String> restResponse = restTemplate.getForEntity(providerUrl, String.class);
Then parse the response body yourself?
I am new to Client side of microservice architecture. I am building a rest based client to submit mine form data using above piece of code.
public ResponseEntity<?> saveCustomerInfo(RegisterCustomer customerModel) {
RestTemplate restTemp = new RestTemplate();
String url= "http://localhost:8081/applicationDemo/saveCustInfo";
HttpEntity http = new HttpEntity(customerModel);
System.out.println(customerModel);
ResponseEntity resp =
restTemp.postForEntity(url,http,ResponseEntity.class);
System.out.println(resp.getBody());
return null;
When i test above code using POSTMAN , it show exception as given below :
org.springframework.web.client.HttpClientErrorException: 415 null
I am not able to get what is gone wrong because when i hit micro services without client, it save the data.But when i use this Rest Template based method, it shows above exception.
I want to get the object as well as the HTTP response of one api into another in Spring code. For that I am using a rest template and I am getting the desired Object from it successfully.
But I want to fetch HTTP response also for the respective api.
What should I do to get this?
RestTemplate restTemplate = new RestTemplate();
Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
System.out.println("quote "+quote);
System.out.println(quote.getType());
log.info(quote.toString());
getForObject method of RestTemplate only gets the result. If you're interest in the status code you should invode exchange which returns a ResponseEntity which has a getStatusCode method.
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Quote> response= restTemplate.exchange ("http://gturnquist-quoters.cfapps.io/api/random", HttpMethod.GET, null, Quote.class);
Quote quote = response..getBody();
System.out.println("status "+response..getStatusCode());
System.out.println("quote "+quote);
System.out.println(quote.getType());
I'm not familiar with Spring RestTemplate.
But for this project I have to use Spring RestTemplate to send a POST call to consume a rest api.
I'm using this code:
String restCall = restTemplate.postForObject(url+restParm, null, String.class);
This is working fine.
I would like to retriveve the HTTP status code (E.g: 200 OK.). How could I do that ?
Thanks.
You use the postForEntity method as follows...
ResponseEntity<String> response = restTemplate.postForEntity(url+restParm, null, String.class);
HttpStatus status = response.getStatusCode();
String restCall = response.getBody();
It will be pretty weird if RestTemplate couldn't get the response,as others have suggested. It is simply not true.
You just use the postForEntity method which returns a
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/http/ResponseEntity.html
And as the documentation suggests, the response entity has the status.