java 带有查询参数的 RestTemplate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17144215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:08:06  来源:igfitidea点击:

RestTemplate with Query params

javaspringrest

提问by Nir

I'm using org.springframework.web.client.resttemplate and I need to pass query params to my GET request.

我正在使用 org.springframework.web.client.resttemplate 并且我需要将查询参数传递给我的 GET 请求。

Does anyone have any example of this?

有没有人有这样的例子?

回答by NimChimpsky

Just pass them as part of the url string. Spring will do the rest, shown below are two types of parameter - an uri parameter and a request parameter:

只需将它们作为 url 字符串的一部分传递即可。Spring 将完成剩下的工作,下面显示了两种类型的参数 - 一个 uri 参数和一个请求参数:

String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings?example=stack",String.class,"42");

Docs here.

文档在这里。

回答by noob

While making a request to a RESTful server, it requires in many a cases to send query parameters, request body (in case of POSTand PUTrequest methods), as well as headers in the request to the server.

在向 RESTful 服务器发出请求时,在许多情况下需要向服务器发送查询参数、请求正文(在POSTPUT请求方法的情况下)以及请求中的标头。

In such cases, the URI string can be built using UriComponentsBuilder.build(), encoded using UriComponents.encode()(useful when you want to send JSON or anything that has symbols {and }as part of the params), and sent using RestTemplate.exchange()like this:

在这种情况下,URI字符串可以使用内置UriComponentsBuilder.build()使用,编码UriComponents.encode() (当你想要发送的JSON或任何有符号有益的{,并}作为则params的一部分),并使用发送RestTemplate。 exchange()像这样:

public ResponseEntity<String> requestRestServerWithGetMethod()
{
    HttpEntity<?> entity = new HttpEntity<>(requestHeaders); // requestHeaders is of HttpHeaders type
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(rawValidUrl) // rawValidURl = http://example.com/hotels
            .queryParams(
                    (LinkedMultiValueMap<String, String>) allRequestParams); // The allRequestParams must have been built for all the query params
    UriComponents uriComponents = builder.build().encode(); // encode() is to ensure that characters like {, }, are preserved and not encoded. Skip if not needed.
    ResponseEntity<Object> responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,
            entity, String.class);
    return responseEntity;
}

public ResponseEntity<String> requestRestServerWithPostMethod()
{
    HttpEntity<?> entity = new HttpEntity<>(requestBody, requestHeaders); // requestBody is of string type and requestHeaders is of type HttpHeaders
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(rawValidUrl) // rawValidURl = http://example.com/hotels
            .queryParams(
                    (LinkedMultiValueMap<String, String>) allRequestParams); // The allRequestParams must have been built for all the query params
    UriComponents uriComponents = builder.build().encode(); // encode() is to ensure that characters like {, }, are preserved and not encoded. Skip if not needed.
    ResponseEntity<Object> responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST,
            entity, String.class);
    return responseEntity;
}