Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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 使用SpringRESTTemplate向每个REST请求添加查询参数_Java_Spring_Rest_Spring Boot_Resttemplate - Fatal编程技术网

Java 使用SpringRESTTemplate向每个REST请求添加查询参数

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

有没有办法在Spring中为
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