Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带查询参数的RestTemplate_Java_Spring_Rest - Fatal编程技术网

Java 带查询参数的RestTemplate

Java 带查询参数的RestTemplate,java,spring,rest,Java,Spring,Rest,我正在使用org.springframework.web.client.rest模板 我需要将查询参数传递给GET请求 有人有这样的例子吗?只需将它们作为url字符串的一部分传递即可。Spring将完成其余工作,如下所示为两种类型的参数—uri参数和请求参数: String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings?example=stack",String.class,"42")

我正在使用org.springframework.web.client.rest模板 我需要将查询参数传递给GET请求


有人有这样的例子吗?

只需将它们作为url字符串的一部分传递即可。Spring将完成其余工作,如下所示为两种类型的参数—uri参数和请求参数:

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

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

在这种情况下,可以使用构建URI字符串,使用进行编码(当您想要发送JSON或任何包含符号
{
}
作为参数的一部分时非常有用),并使用如下方式发送:

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;
}
public ResponseEntity requestRestServerWithGetMethod()
{
HttpEntity实体=新的HttpEntity(requestHeaders);//requestHeaders属于HttpHeaders类型
UriComponentsBuilder=UriComponentsBuilder.FromUristing(rawValidUrl)//rawValidUrl=http://example.com/hotels
.queryParams(
(LinkedMultiValueMap)allRequestParams);//必须为所有查询参数生成allRequestParams
UriComponents UriComponents=builder.build().encode();//encode()是为了确保像{,}这样的字符被保留并且没有编码。如果不需要,请跳过。
ResponseEntity ResponseEntity=restTemplate.exchange(uriComponents.toUri(),HttpMethod.GET,
实体、字符串、类);
返回响应性;
}
public ResponseEntity requestRestServerWithPostMethod()
{
HttpEntity实体=新的HttpEntity(requestBody,requestHeaders);//requestBody为字符串类型,requestHeaders为HttpHeaders类型
UriComponentsBuilder=UriComponentsBuilder.FromUristing(rawValidUrl)//rawValidUrl=http://example.com/hotels
.queryParams(
(LinkedMultiValueMap)allRequestParams);//必须为所有查询参数生成allRequestParams
UriComponents UriComponents=builder.build().encode();//encode()是为了确保像{,}这样的字符被保留并且没有编码。如果不需要,请跳过。
ResponseEntity ResponseEntity=restemplate.exchange(uriComponents.toUri(),HttpMethod.POST,
实体、字符串、类);
返回响应性;
}

你的问题没有多大意义。如果您在发送的请求中添加变量,那么它将是POST,我已经在下面回答了。@Jim在get请求中发送url参数是非常明智的,为什么不;不是吗?他们就是这样for@NimChimpsky为了澄清这一点,我认为查询参数是含糊不清的,也许我的评论也是如此。我同意在URL中包含参数是可以接受的,但在HTML请求的主体中不可以。。。。这就是为什么谢谢你的帮助。不过,当您的uri参数之一指向json时,会出现一个问题,例如:?{searchKey}={searchValue}&page={page}&start={start}&limit={limit}&sort=[{property:\“{propertyValue}\,\“direction\:\”{direction}]“@Nir将json作为一个字符串传递,并使用jackson/gson将其转换为pojo服务器端感谢@NumChimpsky->您有此实现的示例吗?