Java 如何在没有uri的情况下将查询传递给rest模板中的url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21505807/
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-08-13 09:03:12  来源:igfitidea点击:

how to pass queries to a url in rest template without uri

javaspringresturlresttemplate

提问by РАВИ

I am trying to make a GET call to a url and I need to pass in queries to get the response i want. I am using spring framework and using Rest template to make the calls. I know i can manually do this way:

我正在尝试对 url 进行 GET 调用,我需要传入查询以获得我想要的响应。我正在使用 spring 框架并使用 Rest 模板进行调用。我知道我可以手动这样做:

Uritemplate(url+name={name}...

but this is a pain. I need a easier way and the hash map will be generated dynamically

但这是一种痛苦。我需要一个更简单的方法,哈希映射将动态生成

So how do i pass in a map to a url without using uri encoder?

那么如何在不使用 uri 编码器的情况下将地图传递给 url?

 String url = "example.com/search
 Map<String, String> params = new HashMap<String, String>();
          params.put("name", "john");
          params.put("location", "africa");

public static ResponseEntity<String> callGetService(String url, Map<String, String> param) {
        RestTemplate rest = new RestTemplate();
        rest.getMessageConverters().add(new MappingHymanson2HttpMessageConverter());

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

        HttpEntity<?> reqentity = new HttpEntity<Object>(headers);

        ResponseEntity<String> resp = rest.exchange(url, HttpMethod.GET, reqentity, String.class);
        System.out.println(resp);
        return resp;
    }

So url will end up like this example.com/search?name=john&location=africa
response: {name:john doe, love: football} --- tons of json data

所以 url 最终会像这样 example.com/search?name=john&location=africa
response: {name:john doe, love: Football} --- 大量的 json 数据

采纳答案by Sotirios Delimanolis

You can use UriComponentsBuilderand UriComponentswhich facilitate making URIs

您可以使用UriComponentsBuilderUriComponents这有助于制作 URI

String url = "http://example.com/search";
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("name", "john");
params.add("location", "africa");

UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(url).queryParams(params).build();
System.out.println(uriComponents.toUri());

prints

印刷

http://example.com/search?name=john&location=africa

There are other options if you need to use URI variables for path segments.

如果您需要为路径段使用 URI 变量,还有其他选项。

Note that if you are sending an HTTP request, you need an valid URL. The HTTP URL schema is explained in the HTTP specification, here. The UriComponentsBuilderprovides methods to build all parts of the URL.

请注意,如果您要发送 HTTP 请求,则需要一个有效的 URL。HTTP URL 架构在 HTTP 规范中进行了解释,这里。该UriComponentsBuilder提供的方法来构建URL的所有部分。