Java 使用SpringRESTTemplate向每个REST请求添加查询参数
有没有办法在Spring中为Java 使用SpringRESTTemplate向每个REST请求添加查询参数,java,spring,rest,spring-boot,resttemplate,Java,Spring,Rest,Spring Boot,Resttemplate,有没有办法在Spring中为RestTemplate执行的每个HTTP请求添加一个查询参数 Atlassian API使用查询参数os_authType来指定身份验证方法,因此我希望将?os_authType=basic附加到每个请求,而不在我的代码中指定它 代码 @Service public class MyService { private RestTemplate restTemplate; @Autowired public MyService(RestTe
RestTemplate
执行的每个HTTP请求添加一个查询参数
Atlassian API使用查询参数os_authType
来指定身份验证方法,因此我希望将?os_authType=basic
附加到每个请求,而不在我的代码中指定它
代码
@Service
public class MyService {
private RestTemplate restTemplate;
@Autowired
public MyService(RestTemplateBuilder restTemplateBuilder,
@Value("${api.username}") final String username, @Value("${api.password}") final String password, @Value("${api.url}") final String url ) {
restTemplate = restTemplateBuilder
.basicAuthorization(username, password)
.rootUri(url)
.build();
}
public ResponseEntity<String> getApplicationData() {
ResponseEntity<String> response
= restTemplate.getForEntity("/demo?os_authType=basic", String.class);
return response;
}
}
@服务
公共类MyService{
私有RestTemplate RestTemplate;
@自动连线
公共MyService(RestTemplateBuilder、RestTemplateBuilder、,
@Value(${api.username})最终字符串用户名,@Value(${api.password}”)最终字符串密码,@Value(${api.url}”)最终字符串url){
restTemplate=restTemplateBuilder
.basicAuthorization(用户名、密码)
.rootUri(url)
.build();
}
公共响应属性getApplicationData(){
反应性反应
=restemplate.getForEntity(“/demo?os_authType=basic”,String.class);
返回响应;
}
}
您可以编写实现clienthttpprequestinterceptor的自定义RequestInterceptor
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
public class AtlassianAuthInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(
HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
// logic to check if request has query parameter else add it
return execution.execute(request, body);
}
}
现在我们需要配置rest模板
来使用它
import java.util.Collections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
@Configuration
public class MyAppConfig {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
restTemplate.setInterceptors(Collections.singletonList(new AtlassianAuthInterceptor()));
return restTemplate;
}
}
对于那些对添加查询参数的逻辑感兴趣的人来说,由于HttpRequest是不可变的,所以需要一个包装器类
class RequestWrapper {
private final HttpRequest original;
private final URI newUriWithParam;
...
public HttpMethod getMethod() { return this.original.method }
public URI getURI() { return newUriWithParam }
}
然后在clienthtprequestinterceptor
中,您可以执行以下操作
public ClientHttpResponse intercept(
request: HttpRequest,
body: ByteArray,
execution: ClientHttpRequestExecution
) {
URI uri = UriComponentsBuilder.fromUri(request.uri).queryParam("new-param", "param value").build().toUri();
return execution.execute(RequestWrapper(request, uri), body);
}
更新
由于Spring3.1包装类
org.springframework.http.client.support.HttpRequestWrapper
在SpringWeb
中可用,rest模板没有参数吗?spring库中已经有一个包装类org.springframework.http.client.support.HttpRequestWrapper