I am using the graph api:
GET /users/{id | userPrincipalName}/photo/$value
to get the particular user's profile photo with my access token. In postman I am able to see the image using the above get call.
In my spring-boot application I am using like below:
final ResponseEntity<Object> profilePicture = restTemplate.exchange(graphUrl, HttpMethod.GET, new HttpEntity<>((header)), new ParameterizedTypeReference<>() {});
I am getting below error:
Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Object] and content type [image/jpeg]
I have defined RestTemplate like:
#Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
Could someone please help me with this?
You need to add an appropriate MessageConverter to your RestTemplate.
Something like:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
ResponseEntity<byte[]> response = restTemplate.exchange(graphUrl,
HttpMethod.GET, new HttpEntity<>((header)), byte[].class);
You can read more on this subject here: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/HttpMessageConverter.html
I am trying to test a Camel route. The route uses the REST DSL to receive POST requests, conduct some processing, and return a value.
My tests are returning odd messages bodies and I'm not sure why.
Here is the test:
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationTest {
#Autowired
private TestRestTemplate restTemplate;
#Autowired
private CamelContext camelContext;
#Test
public void newRequest() throws IOException {
// Then call the REST API
// Read file as a string into a variable 'content'
String content = new String(Files.readAllBytes(Paths.get("test_data.csv")), StandardCharsets.UTF_8);
// Create a rest template to create a (post) request
RestTemplate restTemplate = new RestTemplate();
// Create Mulipart-form data and set the content type to multpart-form data
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
// Create an object called 'body' of type multipart
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
// Set content (of loaded file) into the multipart object
body.add("file", content);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
String serverUrl = "http://localhost:8081/";
ResponseEntity<String> response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);
The response entity body value is with response status code 200:
{
"--_Q_XsVubda334izooe8NkhJMiv11hjg3kHE": "--_Q_XsVubda334izooe8NkhJMiv11hjg3kHE--"
}
I am expecting a JSON with legible key value pairs.
I would appreciate some tips on why I'm getting this response and if i've gone wrong anywhere in my test.
I am trying to learn RestTemplate and for that made two test spring-boot applications, client and server. Tried some examples on google before asking here, and sorry for the duplicate post if I missed it.
#Slf4j
#RestController
public class ServerController {
#PostMapping("/post")
#ResponseBody
public Resource post(#RequestBody Map<String, String> body,
#RequestParam(name = "path", defaultValue = "NAN") String path) {
if (!body.get("key").equalsIgnoreCase("valid")){
return Resource.builder().ip("'0.0.0.0").scope("KEY NOT VALID").serial(0).build();
}
switch (path) {
case "work":
return Resource.builder().ip("115.212.11.22").scope("home").serial(123).build();
case "home":
return Resource.builder().ip("115.212.11.22").scope("home").serial(456).build();
default:
return Resource.builder().ip("127.0.01").scope("local").serial(789).build();
}
}
}
And here is my ClientController
#Slf4j
#RestController
public class ClientController {
#GetMapping("/get")
public Resource get() {
String url = "http://localhost:8085/post";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAccept(Arrays.asList(MediaType.MULTIPART_FORM_DATA));
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("key", "valid");
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(body, httpHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Resource> response = restTemplate.exchange(url, HttpMethod.POST, entity, Resource.class, Collections.singletonMap("path", "home"));
return response.getBody();
}
}
In ClientController I am trying to mimic what I did in Postman but without luck.
Postman PrintScreen
What am I doing wrong? Thank you!
Managed to figure it out. Had to refactor like this
#GetMapping("/get")
public Resource get(){
String url = "http://localhost:8085/post";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("path", "home");
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
HttpEntity<Map<String, String>> request = new HttpEntity<Map<String, String>>(Collections.singletonMap("key", "valid"), httpHeaders);
Resource resource = restTemplate.postForObject(builder.toUriString(), request, Resource.class);
return resource;
}
i got some problems with my SpringBoot REST Controller. This simply does a http GET call to our database and should return a simple String / json. when i call the URL simply in my browser or via my angular 3 app, the response has some charset errors and i don't know, how to fix them.
I suggest, it is a UTF-8 problem.
First to show you the output:
this is how it comes from the Controller: MeinekestraÃe
and it should be Meinekestraße
here is a part of my SpringBoot Controller:
#Controller
public class RecieverController {
#Value("${server}")
private String server;
#Value("${user.token}")
private String token;
#RequestMapping(value="/reciever", method=RequestMethod.GET, produces = "text/plain;charset=UTF-8")
#ResponseBody
#CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
public String getRecieverData(
#RequestHeader(value="Accept") String accept,
#RequestHeader(value="Host") String host) {
final String url = server + "/rest/client/profile";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
headers.set("Auth-Token", token); // user_token
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
return response.getBody();
}}
I tried the following things, but nothing changed in the output.
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
or this
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
any other ideas what could be the problem? the database isnt the issue. Everything is stored correctly there.
Edit:
this is the screenshot of the header from the output
and a part from the json output:
The Problem could be solved by adding both
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
and this
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
Thanks #dienerd for helping me via chat
For the ones looking for a way to force encoding request/response in #RestController in a Spring Boot project.
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
is deprecated, in your application.yaml or application.properties use the following:
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true
This did the trick for me.
I am using RestTemplate to make an HTTP call to our service which returns a simple JSON response. I don't need to parse that JSON at all. I just need to return whatever I am getting back from that service.
So I am mapping that to String.class and returning the actual JSON response as a string.
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(url, String.class);
return response;
Now the question is -
I am trying to extract HTTP Status codes after hitting the URL. How can I extract HTTP Status code from the above code? Do I need to make any change into that in the way I doing it currently?
Update:-
This is what I have tried and I am able to get the response back and status code as well. But do I always need to set HttpHeaders and Entity object like below I am doing it?
RestTemplate restTemplate = new RestTemplate();
//and do I need this JSON media type for my use case?
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//set my entity
HttpEntity<Object> entity = new HttpEntity<Object>(headers);
ResponseEntity<String> out = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
System.out.println(out.getBody());
System.out.println(out.getStatusCode());
Couple of question - Do I need to have MediaType.APPLICATION_JSON as I am just making a call to url which returns a response back, it can return either JSON or XML or simple string.
Use the RestTemplate#exchange(..) methods that return a ResponseEntity. This gives you access to the status line and headers (and the body obviously).
getStatusCode()
getHeaders()
If you don´t want to leave the nice abstraction around RestTemplate.get/postForObject... methods behind like me and dislike to fiddle around with the boilerplate stuff needed when using RestTemplate.exchange... (Request- and ResponseEntity, HttpHeaders, etc), there´s another option to gain access to the HttpStatus codes.
Just surround the usual RestTemplate.get/postForObject... with a try/catch for org.springframework.web.client.HttpClientErrorException and org.springframework.web.client.HttpServerErrorException, like in this example:
try {
return restTemplate.postForObject("http://your.url.here", "YourRequestObjectForPostBodyHere", YourResponse.class);
} catch (HttpClientErrorException | HttpServerErrorException httpClientOrServerExc) {
if(HttpStatus.NOT_FOUND.equals(httpClientOrServerExc.getStatusCode())) {
// your handling of "NOT FOUND" here
// e.g. throw new RuntimeException("Your Error Message here", httpClientOrServerExc);
}
else {
// your handling of other errors here
}
The org.springframework.web.client.HttpServerErrorException is added here for the errors with a 50x.
Now you´re able to simple react to all the StatusCodes you want - except the appropriate one, that matches your HTTP method - like GET and 200, which won´t be handled as exception, as it is the matching one. But this should be straight forward, if you´re implementing/consuming RESTful services :)
If you want all the HTTPStatus from a RestTemplate including 4XX and 5XX, you will have to provide an ResponseErrorHandler to the restTemplate, since the default handler will throw an exception in case of 4XX or 5XX
We could do something like that :
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
#Override
public boolean hasError(HttpStatus statusCode) {
return false;
}
});
ResponseEntity<YourResponse> responseEntity =
restTemplate.getForEntity("http://your.url.here", YourResponse.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.XXXX);
private RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(url,HttpMethod.GET, requestEntity,String.class);
response contains 'body', 'headers' and 'statusCode'
to get statusCode : response.getStatusCode();
exchange(...) works but if you want less code, you can use
org.springframework.boot.test.web.client.TestRestTemplate.getForEntity(...)
which returns an Entity containing StatusCode. Change your example code to this:
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
HttpStatus statusCode = response.getStatusCode();
To test it you can use this snippet from my unit test:
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
assertResponseHeaderIsCorrect(response, HttpStatus.OK);
/**
* Test the basics of the response, non-null, status expected, etc...
*/
private void assertResponseHeaderIsCorrect(ResponseEntity<String> response, HttpStatus expectedStatus) {
assertThat(response).isNotNull();
assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON_UTF8);
assertThat(response.getStatusCode()).isEqualTo(expectedStatus);
}
There can be some slightly trickier use cases someone might fall in (as I did). Consider the following:
Supporting a Page object in order to use it with RestTemplate and ParameterizedTypeReference:
RestPageResponse:
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
public class RestResponsePage<T> extends PageImpl<T>{
private static final long serialVersionUID = 3248189030448292002L;
public RestResponsePage(List<T> content, Pageable pageable, long total) {
super(content, pageable, total);
}
public RestResponsePage(List<T> content) {
super(content);
}
public RestResponsePage() {
super(new ArrayList<T>());
}
}
Using ParameterizedTypeReference will yield the following:
ParameterizedTypeReference<RestResponsePage<MyObject>> responseType =
new ParameterizedTypeReference<RestResponsePage<MyObject>>() {};
HttpEntity<RestResponsePage<MyObject>> response = restTemplate.exchange(oauthUrl, HttpMethod.GET, entity, responseType);
Calling #exchange:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<?> entity = new HttpEntity<>(headers);
response = restTemplate.exchange("localhost:8080/example", HttpMethod.GET, entity, responseType);
Now here is the "tricky" part.
Trying to call exchange's getStatusCode will be impossible because the compiler, unfortunately, will be unaware of the "intended" type of response.
That is because generics are implemented via type erasure which removes all information regarding generic types during compilation (read more - source)
((ResponseEntity<RestResponsePage<MyObject>>) response).getStatusCode()
In this case, you have to explicitly cast the variable to the desired Class to get the statusCode (and/or other attributes)!
Putting this much of code is enough for me
HttpStatus statusCode = ((ResponseEntity<Object>) responseOfEsoft).getStatusCode();
You can use this solution
RestTemplate restTemplate = new RestTemplate();
final String baseUrl = "http://www.myexampleurl.com";
URI uri = new URI(baseUrl);
ResponseEntity<String> result = restTemplate.getForEntity(uri, String.class);
//get status code
int statuCode = result.getStatusCodeValue();
Was able to solve this through:
HttpEntity<Object> entity = restTemplate.getForEntity(uri, Object.class);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
System.out.println(result.getStatusCode());