Spring RestTemplate GET with parameters - java

I have to make a REST call that includes custom headers and query parameters. I set my HttpEntity with just the headers (no body), and I use the RestTemplate.exchange() method as follows:
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
Map<String, String> params = new HashMap<String, String>();
params.put("msisdn", msisdn);
params.put("email", email);
params.put("clientVersion", clientVersion);
params.put("clientType", clientType);
params.put("issuerName", issuerName);
params.put("applicationName", applicationName);
HttpEntity entity = new HttpEntity(headers);
HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params);
This fails at the client end with the dispatcher servlet being unable to resolve the request to a handler. Having debugged it, it looks like the request parameters are not being sent.
When I do a an exchange with a POST using a request body and no query parameters it works just fine.
Does anyone have any ideas?

To easily manipulate URLs / path / params / etc., you can use Spring's UriComponentsBuilder class to create a URL template with placehoders for the parameters, then provide the value for those parameters in the RestOperations.exchange(...) call. It's cleaner than manually concatenating strings and it takes care of the URL encoding for you:
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
HttpEntity<?> entity = new HttpEntity<>(headers);
String urlTemplate = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("msisdn", "{msisdn}")
.queryParam("email", "{email}")
.queryParam("clientVersion", "{clientVersion}")
.queryParam("clientType", "{clientType}")
.queryParam("issuerName", "{issuerName}")
.queryParam("applicationName", "{applicationName}")
.encode()
.toUriString();
Map<String, ?> params = new HashMap<>();
params.put("msisdn", msisdn);
params.put("email", email);
params.put("clientVersion", clientVersion);
params.put("clientType", clientType);
params.put("issuerName", issuerName);
params.put("applicationName", applicationName);
HttpEntity<String> response = restOperations.exchange(
urlTemplate,
HttpMethod.GET,
entity,
String.class,
params
);

The uriVariables are also expanded in the query string. For example, the following call will expand values for both, account and name:
restTemplate.exchange("http://my-rest-url.org/rest/account/{account}?name={name}",
HttpMethod.GET,
httpEntity,
clazz,
"my-account",
"my-name"
);
so the actual request url will be
http://my-rest-url.org/rest/account/my-account?name=my-name
Look at HierarchicalUriComponents.expandInternal(UriTemplateVariables) for more details.
Version of Spring is 3.1.3.

Since at least Spring 3, instead of using UriComponentsBuilder to build the URL (which is a bit verbose), many of the RestTemplate methods accept placeholders in the path for parameters (not just exchange).
From the documentation:
Many of the RestTemplate methods accepts a URI template and URI
template variables, either as a String vararg, or as
Map<String,String>.
For example with a String vararg:
restTemplate.getForObject(
"http://example.com/hotels/{hotel}/rooms/{room}", String.class, "42", "21");
Or with a Map<String, String>:
Map<String, String> vars = new HashMap<>();
vars.put("hotel", "42");
vars.put("room", "21");
restTemplate.getForObject("http://example.com/hotels/{hotel}/rooms/{room}",
String.class, vars);
Reference: https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#rest-resttemplate-uri
If you look at the JavaDoc for RestTemplate and search for "URI Template", you can see which methods you can use placeholders with.

OK, so I'm being an idiot and I'm confusing query parameters with url parameters. I was kinda hoping there would be a nicer way to populate my query parameters rather than an ugly concatenated String but there we are. It's simply a case of build the URL with the correct parameters. If you pass it as a String Spring will also take care of the encoding for you.

String uri = http://my-rest-url.org/rest/account/{account};
Map<String, String> uriParam = new HashMap<>();
uriParam.put("account", "my_account");
UriComponents builder = UriComponentsBuilder.fromHttpUrl(uri)
.queryParam("pageSize","2")
.queryParam("page","0")
.queryParam("name","my_name").build();
HttpEntity<String> requestEntity = new HttpEntity<>(null, getHeaders());
ResponseEntity<String> strResponse = restTemplate.exchange(builder.toUriString(),HttpMethod.GET, requestEntity,
String.class,uriParam);
//final URL: http://my-rest-url.org/rest/account/my_account?pageSize=2&page=0&name=my_name
RestTemplate: Build dynamic URI using UriComponents (URI variable and Request parameters)

I was attempting something similar, and the RoboSpice example helped me work it out:
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
HttpEntity<String> request = new HttpEntity<>(input, createHeader());
String url = "http://awesomesite.org";
Uri.Builder uriBuilder = Uri.parse(url).buildUpon();
uriBuilder.appendQueryParameter(key, value);
uriBuilder.appendQueryParameter(key, value);
...
String url = uriBuilder.build().toString();
HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request , String.class);

Converting of a hash map to a string of query parameters:
Map<String, String> params = new HashMap<>();
params.put("msisdn", msisdn);
params.put("email", email);
params.put("clientVersion", clientVersion);
params.put("clientType", clientType);
params.put("issuerName", issuerName);
params.put("applicationName", applicationName);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.queryParam(entry.getKey(), entry.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
HttpEntity<String> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, new HttpEntity(headers), String.class);

In Spring Web 4.3.6 I also see
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables)
That means you don't have to create an ugly map
So if you have this url
http://my-url/action?param1={param1}&param2={param2}
You can either do
restTemplate.getForObject(url, Response.class, param1, param2)
or
restTemplate.getForObject(url, Response.class, param [])

I take different approach, you may agree or not but I want to control from .properties file instead of compiled Java code
Inside application.properties file
endpoint.url = https://yourHost/resource?requestParam1={0}&requestParam2={1}
Java code goes here, you can write if or switch condition to find out if endpoint URL in .properties file has #PathVariable (contains {}) or #RequestParam (yourURL?key=value) etc... then invoke method accordingly... that way its dynamic and not need to code change in future one stop shop...
I'm trying to give more of idea than actual code here ...try to write generic method each for #RequestParam, and #PathVariable etc... then call accordingly when needed
#Value("${endpoint.url}")
private String endpointURL;
// you can use variable args feature in Java
public String requestParamMethodNameHere(String value1, String value2) {
RestTemplate restTemplate = new RestTemplate();
restTemplate
.getMessageConverters()
.add(new MappingJackson2HttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
HttpEntity<String> entity = new HttpEntity<>(headers);
try {
String formatted_URL = MessageFormat.format(endpointURL, value1, value2);
ResponseEntity<String> response = restTemplate.exchange(
formatted_URL ,
HttpMethod.GET,
entity,
String.class);
return response.getBody();
} catch (Exception e) { e.printStackTrace(); }

If you pass non-parametrized params for RestTemplate, you'll have one Metrics for everyone single different URL that you pass, considering the parameters. You would like to use parametrized urls:
http://my-url/action?param1={param1}&param2={param2}
instead of
http://my-url/action?param1=XXXX&param2=YYYY
The second case is what you get by using UriComponentsBuilder class.
One way to implement the first behavior is the following:
Map<String, Object> params = new HashMap<>();
params.put("param1", "XXXX");
params.put("param2", "YYYY");
String url = "http://my-url/action?%s";
String parametrizedArgs = params.keySet().stream().map(k ->
String.format("%s={%s}", k, k)
).collect(Collectors.joining("&"));
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
HttpEntity<String> entity = new HttpEntity<>(headers);
restTemplate.exchange(String.format(url, parametrizedArgs), HttpMethod.GET, entity, String.class, params);

public static void main(String[] args) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Accept", MediaType.APPLICATION_JSON_VALUE);
final String url = "https://host:port/contract/{code}";
Map<String, String> params = new HashMap<String, String>();
params.put("code", "123456");
HttpEntity<?> httpEntity = new HttpEntity<>(httpHeaders);
RestTemplate restTemplate = new RestTemplate();
restTemplate.exchange(url, HttpMethod.GET, httpEntity,String.class, params);
}

I am providing a code snippet of RestTemplate GET method with path param example
public ResponseEntity<String> getName(int id) {
final String url = "http://localhost:8080/springrestexample/employee/name?id={id}";
Map<String, String> params = new HashMap<String, String>();
params.put("id", id);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity request = new HttpEntity(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, String.class, params);
return response;
}

You can use follow code for String.
URL_EXAMPLE="http://{domain}/Index.php?Username={user}&password={password}";
String domain = "example.com";
String user = "user";
String password = "password";
String data=this.restTemplate.getForObject(URL_EXAMPLE,String.class,domain,user,password);

If your url is http://localhost:8080/context path?msisdn={msisdn}&email={email}
then
Map<String,Object> queryParams=new HashMap<>();
queryParams.put("msisdn",your value)
queryParams.put("email",your value)
works for resttemplate exchange method as described by you

hi i build url with query params using this code:
UriComponentsBuilder.fromHttpUrl(url)
.queryParam("bikerPhoneNumber", "phoneNumberString")
.toUriString();

One more solution as method:
private String execute(String url, Map<String, String> params) {
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(url)
// predefined params
.queryParam("user", "userValue")
.queryParam("password", "passwordValue");
params.forEach(uriBuilder::queryParam);
HttpHeaders headers = new HttpHeaders() {{
setContentType(MediaType.APPLICATION_FORM_URLENCODED);
setAccept(List.of(MediaType.APPLICATION_JSON));
}};
ResponseEntity<String> request = restTemplate.exchange(uriBuilder.toUriString(),
HttpMethod.GET, new HttpEntity<>(headers), String.class);
return request.getBody();
}

Related

How to pass Headers and request body in POST request?

I am using RestTemplate restTemplate.exchangemethod to POST request to an endpoint. I have OAuth Header and HttpEntity in different file which I want to pass to POST request, in addition to this I also want to pass request to the endpoint.
I was able to successfully pass the headers and request, but not Http entity which contains credentials
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST,
new HttpEntity<>(request, dataRepo.getHeader()), String.class);
Is there any way I can pass all 3 things
HttpEntity
HttpHeaders
request
Here is my code
#RunWith(MockitoJUnitRunner.class)
#TestPropertySource
public class DataTest {
#Inject
private Oauth oauth;
#Mock
private DataRepo dataRepo;
RestTemplate restTemplate = new RestTemplate();
#Qualifier(OAuth2HttpHeadersBuilder.BEAN_NAME)
NewHttpHeader headersBuilder;
#Test
public void testAddEmployeeSuccess() throws URISyntaxException {
URI uri = new URI(url);
Set<String> mockData = Stream.of("A","B").collect(Collectors.toSet());
String onsString = String.join(",", mockData);
Map<String, String> requestBody = new HashMap<>();
requestBody.put("name", onsString);
JSONObject jsonObject = new JSONObject(requestBody);
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), null);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST,
new HttpEntity<>(request, dataRepo.getHeader()), String.class);
Assert.assertEquals(201, result.getStatusCodeValue());
}
The below code is in NewHttpHeader.java file which contains
Header and HttpEntity
private HttpEntity<MultiValueMap<String,String>> getHttpEntity() {
MultiValueMap<String, String> store = new LinkedMultiValueMap<>();
store.add( "pas", "password" );
store.add( "name", config.getVaultServiceAccountName() );
return new HttpEntity<>( store, getHeader() );
}
private HttpHeaders getHeader() {
HttpHeaders httpHeaders = headersBuilder.build();
httpHeaders.add( HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType() );
httpHeaders.add( HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType() );
return httpHeaders;
}
}
Quoting question:
Is there any way I can pass all 3 things
HttpEntity
HttpHeaders
request
Quoting javadoc of HttpEntity:
Represents an HTTP request or response entity, consisting of headers and body.
So the answer to your question is: Yes, you can pass all 3, since the first is nothing but a combination of the other two.
Just merge your two HttpEntity objects.
Before
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), null);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST,
new HttpEntity<>(request, dataRepo.getHeader()), String.class);
After
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), dataRepo.getHeader());
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST,
request, String.class);

Spring Rest Template creating multipart form/data client working like a postman throws non convertable exception

I want to turn this postman client with multipart/form-data header request to a spring template client.
Right now, I have a basic rest controller which works well.
#RestController
#RequestMapping("/api")
public class MainConroller {
private static final Logger log = LoggerFactory.getLogger(MainConroller.class);
#Autowired
private MainService mainService;
public MainConroller(MainService mainService) {
this.mainService = mainService;
}
#PostMapping("/mails/send")
public void send(
#RequestParam("usertoken") String usertoken,
#RequestParam("sendTo") String sendTo,
#RequestParam("subject") String subject,
#RequestParam("content") String content,
#RequestParam(required = false, name = "files") List<MultipartFile> multipartFiles) {
log.debug("{}, {}, {}, {}", usertoken, sendTo, subject, content);
mainService.processMessage(usertoken, sendTo, subject, content, multipartFiles);
}
}
I need to, however, create a rest client so I used a rest template, it right now looks like this:
ArrayList<HttpMessageConverter<?>> converters = new ArrayList<>(
Arrays.asList(new MappingJackson2HttpMessageConverter(), new ResourceHttpMessageConverter(), new FormHttpMessageConverter()));
#Bean
public RestTemplate restTemplate() {
return new RestTemplate(converters);
}
File file = new File("*********");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> fileValueMap = new LinkedMultiValueMap<>();
fileValueMap.add(file.getName(), file);
fileValueMap.add(file.getName(), file);
fileValueMap.add(file.getName(), file);
HttpEntity<MultiValueMap<String, Object>> filesPart = new HttpEntity<>(fileValueMap, httpHeaders);
// String key values part
MultiValueMap<String, String> stringPart = new LinkedMultiValueMap<>();
stringPart.add("usertoken", "test");
stringPart.add("sendTo", "test");
stringPart.add("subject", "test");
stringPart.add("content", "test");
HttpEntity<MultiValueMap<String, String>> objectPart = new HttpEntity<>(stringPart, httpHeaders);
MultiValueMap<String, Object> multiPartRequest = new LinkedMultiValueMap<>();
multiPartRequest.add("ObjectPart", objectPart);
multiPartRequest.add("FilesPart", filesPart);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multiPartRequest, httpHeaders);
String serverUrl = "****";
restTemplate().postForEntity(serverUrl, requestEntity, String.class);
The problem is when I try to send the post request, It throws
Exception in thread "main" org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [multipart/form-data]
Update
The solution on the client side is very simple, you can just send String values in the object values, which are going to be automatically casted to Strings via generics.
Also, the files cannot be send just as files but you have to create FileSystemResource instead, here is the complete code of the client side:
#Service
public class RestTemplatePost {
#Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public void prepareMessage() throws Exception {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("usertoken", "test");
form.add("sendTo", "test");
form.add("subject", "test");
form.add("content", "test");
form.add("files", getTestFile());
form.add("files", getTestFile());
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(form, httpHeaders);
String serverUrl = "******";
restTemplate().postForEntity(serverUrl, requestEntity, String.class);
}
public static Resource getTestFile() throws IOException {
Path testFile = Paths.get("C****");
System.out.println("Creating and Uploading Test File: " + testFile);
Files.write(testFile, "Hello World !!, This is a test file.".getBytes());
return new FileSystemResource(testFile.toFile());
}
}
You are making things too complex. You should use a single map to hold the form values not a map of maps. Next to that Spring Boot already provides a RestTemplate so you don't need to configure your own again.
File file = new File("*********");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("files", file);
form.add("files", file);
form.add("files", file);
form.add("usertoken", "test");
form.add("sendTo", "test");
form.add("subject", "test");
form.add("content", "test");
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(form, httpHeaders);
String serverUrl = "****";
restTemplate().postForEntity(serverUrl, requestEntity, String.class);
The RestTemplate (or actually the FormHttpMessageConverter) will transform it into a correct request.
The default RestTemplate constructor does not include any message converters, you need to add it. For example, you can do it like:
HttpEntity<MultiValueMap<String, Object>> requestEntity = new
HttpEntity<MultiValueMap<String, Object>>(parts, requestHeaders);
RestTemplate restTemplate = getRestTemplate();
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
return restTemplate.postForObject(apiURI, requestEntity, String.class);

RestTemplate GET method with path variables

I am trying to make rest call using rest template. I have two header parameters and one path variable to set for the API call. Below is my implementation. But I am receiving HttpServerErrorException: 500 null. Am I setting the path variable in the right way?
Target API: drs/v1/{caseId}
String url = configProperties.getCaseCreateUrl();
if(!StringUtils.isEmpty(url)){
url = url.replace(ApplicationConstants.DOMAIN_NAME,currentUser.domainUrl());
url = url+ caseId;
}
HttpHeaders headers = new HttpHeaders();
headers.set(ApplicationConstants.PS_TOKEN_HEADER, currentUser.getToken());
headers.set(ApplicationConstants.WORGROUP_HEADER, currentUser.getWorkgroupId());
headers.set(ApplicationConstants.DOMAIN_HEADER, currentUser.getDomainId());
headers.set(HttpHeaders.CONTENT_TYPE, MediaType.ALL_VALUE);
HttpsTrustManager.allowAllSSL();
RestTemplate restTemplate = new RestTemplate();
HttpEntity<Map<String, String>> requestEntity = new HttpEntity(null, headers);
ResponseEntity<CaseDetailsDTO> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, CaseDetailsDTO.class);
I am providing a code snippet of RestTemplate GET method with path variables example
public ResponseEntity<List<String>> getNames(long id) {
final String url = "https://some/{id}/name";
//with cookies
ResponseEntity<String> cookie = getCookies();
String set_cookie = cookie.getHeaders().getFirst(HttpHeaders.SET_COOKIE);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Cookie", set_cookie);
HttpEntity request = new HttpEntity(headers);
ResponseEntity<List<String>> response = restTemplate.exchange(url, HttpMethod.GET, request,new ParameterizedTypeReference<List<String>>(){},id);
return response;
}

Spring RestTemplate POST Request with URL encoded data

I'm new to Spring and trying to do a rest request with RestTemplate. The Java code should do the same as below curl command:
curl --data "name=feature&color=#5843AD" --header "PRIVATE-TOKEN: xyz" "https://someserver.com/api/v3/projects/1/labels"
But the server rejects the RestTemplate with a 400 Bad Request
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("PRIVATE-TOKEN", "xyz");
HttpEntity<String> entity = new HttpEntity<String>("name=feature&color=#5843AD", headers);
ResponseEntity<LabelCreationResponse> response = restTemplate.exchange("https://someserver.com/api/v3/projects/1/labels", HttpMethod.POST, entity, LabelCreationResponse.class);
Can somebody tell me what I'm doing wrong?
I think the problem is that when you try to send data to server didn't set the content type header which should be one of the two: "application/json" or "application/x-www-form-urlencoded" . In your case is: "application/x-www-form-urlencoded" based on your sample params (name and color). This header means "what type of data my client sends to server".
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("PRIVATE-TOKEN", "xyz");
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("name","feature");
map.add("color","#5843AD");
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(map, headers);
ResponseEntity<LabelCreationResponse> response =
restTemplate.exchange("https://foo/api/v3/projects/1/labels",
HttpMethod.POST,
entity,
LabelCreationResponse.class);
You need to set the Content-Type to application/json. Content-Type has to be set in the request. Below is the modified code to set the Content-Type
final String uri = "https://someserver.com/api/v3/projects/1/labels";
String input = "US";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("PRIVATE-TOKEN", "xyz");
HttpEntity<String> request = new HttpEntity<String>(input, headers);
ResponseEntity<LabelCreationResponse> response = restTemplate.postForObject(uri, request, LabelCreationResponse.class);
Here, HttpEntity is constructed with your input i.e "US" and with headers.
Let me know if this works, if not then please share the exception.
Cheers!
It may be a Header issue check if the header is a Valid header, are u referring to "BasicAuth" header?
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED.toString());
headers.add("Accept", MediaType.APPLICATION_JSON.toString()); //Optional in case server sends back JSON data
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<String, String>();
requestBody.add("name", "feature");
requestBody.add("color", "#5843AD");
HttpEntity formEntity = new HttpEntity<MultiValueMap<String, String>>(requestBody, headers);
ResponseEntity<LabelCreationResponse> response =
restTemplate.exchange("https://example.com/api/request", HttpMethod.POST, formEntity, LabelCreationResponse.class);
my issue, the MessageConverters contains other converters may converts then entity to json (like FastJsonHttpMessageConverter). So i added the FormHttpMessageConverter to ahead and it works well.
<T> JuheResult<T> postForm(final String url, final MultiValueMap<String, Object> body) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
return exchange(url, HttpMethod.POST, requestEntity);
}
<T> JuheResult<T> exchange(final String url, final HttpMethod method, final HttpEntity<?> requestEntity) {
ResponseEntity<JuheResult<T>> response = restTemplate.exchange(url, method, requestEntity,
new JuheResultTypeReference<>());
logger.debug("调用结果 {}", response.getBody());
return response.getBody();
}
public JuheSupplierServiceImpl(RestTemplateBuilder restTemplateBuilder) {
Duration connectTimeout = Duration.ofSeconds(5);
Duration readTimeout = Duration.ofSeconds(5);
restTemplate = restTemplateBuilder.setConnectTimeout(connectTimeout).setReadTimeout(readTimeout)
.additionalInterceptors(interceptor()).build();
restTemplate.getMessageConverters().add(0, new FormHttpMessageConverter());
}
fastjson prevent resttemplate converting other mediaTypes other than json

How to get Response Header information from Spring RestTemplate GET request

I am triggering a GET request and getting the JSON data successfully via Spring RestTemplate. I also want to get the Response Header information but I am not sure how to get it.
private String getAPIKeySpring() {
RestTemplate restTemplate = new RestTemplate();
String url = baseURL+"/users/apikey";
Map<String, String> vars = new HashMap<String, String>();
vars.put("X-Auth-User", apiUser);
JsonVO jsonVO = restTemplate.getForObject(url, JsonVO.class, vars);
System.out.println(jsonVO);
return null;
}
ResponseEntity<JsonVO> responseEntity = restTemplate.getForEntity(url, JsonVO.class, vars);
JsonVO jsonVO = responseEntity.getBody();
HttpHeaders headers = responseEntity.getHeaders(); //<-- your headers

Categories

Resources