Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 460 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring RestTemplate GET with parameters

#11
Converting of a hash map to a string of query parameters:

```java
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);
```
Reply

#12
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:

[To see links please register here]


If you look at the [JavaDoc][1] for `RestTemplate` and search for "URI Template", you can see which methods you can use placeholders with.


[1]:

[To see links please register here]

Reply

#13
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;
}
Reply

#14
To easily manipulate URLs / path / params / etc., you can use Spring's [UriComponentsBuilder](

[To see links please register here]

) 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
);

Reply

#15
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);
Reply

#16
hi i build url with query params using this code:

UriComponentsBuilder.fromHttpUrl(url)
.queryParam("bikerPhoneNumber", "phoneNumberString")
.toUriString();
Reply

#17

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();

}
Reply

#18
See I observe two things, the query parameter (@RequestParam) or the dynamic URLs (@PathVariable) can be passed in two ways i.e.
1. Passing the data as Array.
2. Using the map.

In both the pattern, one thing is common.
the data needs to passed dynamically and we can mark those places using curly braces.{}
ex.


[To see links please register here]

}


**Spring Documentation**
Now we need to pass the data,

**Style 1**
```
Map<String,String> data=new LinkedHashMap<String,String>();
data.put("api_key", api_key);
template.getForObject(url, TMDB.class,data);
```


**Style 2**
```
Object[] arr= {api_key};
template.getForObject(url, TMDB.class,arr);
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through