RestTemplate:如何一起发送URL和查询参数

时间:2023-01-13 10:31:23

I am trying to pass path param and query params in a URL but I am getting a weird error. below is the code

我试图传递路径参数和查询URL中的参数,但我得到一个奇怪的错误。下面是代码

    String url = "http://test.com/Services/rest/{id}/Identifier"
    Map<String, String> params = new HashMap<String, String>();
    params.put("id", "1234");
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)
                                        .queryParam("name", "myName");
    String uriBuilder = builder.build().encode().toUriString();
    restTemplate.exchange(uriBuilder , HttpMethod.PUT, requestEntity,
                    class_p, params);

and my url is becoming http://test.com/Services/rest/%7Bid%7D/Identifier?name=myName

我的网址正在变成http://test.com/Services/rest/%7Bid%7D/Identifier?name=myName

what should I do to make it work. I am expecting http://test.com/Services/rest/{id}/Identifier?name=myName so that params will add id to the url

我该怎么做才能让它发挥作用。我期待http://test.com/Services/rest/{id}/Identifier?name=myName,以便params将id添加到网址

please suggest. thanks in Advance

请建议。提前致谢

2 个解决方案

#1


69  

I would use buildAndExpand from UriComponentsBuilder to pass all types of URI parameters.

我将使用UriComponentsBuilder中的buildAndExpand来传递所有类型的URI参数。

For example:

String url = "http://test.com/solarSystem/planet/{planet}/moon/{moon}";

// URI (URL) parameters
Map<String, String> uriParams = new HashMap<String, String>();
uriParams.put("planet", "Mars");
uriParams.put("moon", "Phobos");

// Query parameters
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)
        // Add query parameter
        .queryParam("firstName", "Mark")
        .queryParam("lastName", "Watney");

System.out.println(builder.buildAndExpand(uriParams).toUri());
/**
 * Console output:
 * http://test.com/solarSystem/planet/Mars/moon/Phobos?firstName=Mark&lastName=Watney
 */

restTemplate.exchange(builder.buildAndExpand(uriParams).toUri() , HttpMethod.PUT,
        requestEntity, class_p);

/**
 * Log entry:
 * org.springframework.web.client.RestTemplate Created PUT request for "http://test.com/solarSystem/planet/Mars/moon/Phobos?firstName=Mark&lastName=Watney"
 */

#2


0  

An issue with the answer from Michal Foksa is that it adds the query parameters first, and then expands the path variables. If query parameter contains parenthesis, e.g. {foobar}, this will cause an exception.

Michal Foksa的答案问题是它首先添加查询参数,然后扩展路径变量。如果查询参数包含括号,例如{foobar},这会导致异常。

The safe way is to expand the path variables first, and then add the query parameters:

安全的方法是首先扩展路径变量,然后添加查询参数:

String url = "http://test.com/Services/rest/{id}/Identifier";
Map<String, String> params = new HashMap<String, String>();
params.put("id", "1234");
URI uri = UriComponentsBuilder.fromUriString(url)
        .buildAndExpand(params)
        .toUri();
uri = UriComponentsBuilder
        .fromUri(uri)
        .queryParam("name", "myName")
        .build()
        .toUri();
restTemplate.exchange(uri , HttpMethod.PUT, requestEntity, class_p);

#1


69  

I would use buildAndExpand from UriComponentsBuilder to pass all types of URI parameters.

我将使用UriComponentsBuilder中的buildAndExpand来传递所有类型的URI参数。

For example:

String url = "http://test.com/solarSystem/planet/{planet}/moon/{moon}";

// URI (URL) parameters
Map<String, String> uriParams = new HashMap<String, String>();
uriParams.put("planet", "Mars");
uriParams.put("moon", "Phobos");

// Query parameters
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)
        // Add query parameter
        .queryParam("firstName", "Mark")
        .queryParam("lastName", "Watney");

System.out.println(builder.buildAndExpand(uriParams).toUri());
/**
 * Console output:
 * http://test.com/solarSystem/planet/Mars/moon/Phobos?firstName=Mark&lastName=Watney
 */

restTemplate.exchange(builder.buildAndExpand(uriParams).toUri() , HttpMethod.PUT,
        requestEntity, class_p);

/**
 * Log entry:
 * org.springframework.web.client.RestTemplate Created PUT request for "http://test.com/solarSystem/planet/Mars/moon/Phobos?firstName=Mark&lastName=Watney"
 */

#2


0  

An issue with the answer from Michal Foksa is that it adds the query parameters first, and then expands the path variables. If query parameter contains parenthesis, e.g. {foobar}, this will cause an exception.

Michal Foksa的答案问题是它首先添加查询参数,然后扩展路径变量。如果查询参数包含括号,例如{foobar},这会导致异常。

The safe way is to expand the path variables first, and then add the query parameters:

安全的方法是首先扩展路径变量,然后添加查询参数:

String url = "http://test.com/Services/rest/{id}/Identifier";
Map<String, String> params = new HashMap<String, String>();
params.put("id", "1234");
URI uri = UriComponentsBuilder.fromUriString(url)
        .buildAndExpand(params)
        .toUri();
uri = UriComponentsBuilder
        .fromUri(uri)
        .queryParam("name", "myName")
        .build()
        .toUri();
restTemplate.exchange(uri , HttpMethod.PUT, requestEntity, class_p);