Java 无法通过RestTemplate.getForObject传递标题
我是使用Spring的新手。我正在编写一段代码,试图使用restTemplate.getForObject传递标题 客户端:Java 无法通过RestTemplate.getForObject传递标题,java,spring,Java,Spring,我是使用Spring的新手。我正在编写一段代码,试图使用restTemplate.getForObject传递标题 客户端: String userServiceUrl = "http://localhost:8080/SampleRest/api/user/"; HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", UUID.randomUUID().toString() ); restTemplate.ge
String userServiceUrl = "http://localhost:8080/SampleRest/api/user/";
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", UUID.randomUUID().toString() );
restTemplate.getForObject(userServiceUrl + "{id}",User.class,HttpMethod.GET,headers) ;
但是,在服务器上,根本不传递请求头“身份验证”。没有标题“身份验证”
我无法使用restTemplate.exchange
我做错了什么
非常感谢您的帮助在
restemplate
的getForObject
方法中没有传递头的选项
如果不想使用
exchange
,可以实现clienthtprequestinterceptor
来设置标题。您还可以覆盖SimpleClientHttpRequestFactory
我们可以在Spring boot for GET方法中以以下方式使用它:
@SpringBoot应用程序
公共类应用程序实现CommandLineRunner{
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
try{
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders = this.createHeaders();
ResponseEntity<String> response;
response = restTemplate.exchange("<url>",HttpMethod.GET,new HttpEntity<Object>(httpHeaders),String.class);
log.info(response.toString());
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
private HttpHeaders createHeaders(){
HttpHeaders headers = new HttpHeaders(){
{
set( "Authorization", "3ee140");
}
};
return headers;
}
private static final Logger log=LoggerFactory.getLogger(Application.class);
公共静态void main(字符串[]args){
SpringApplication.run(Application.class,args);
}
@凌驾
公共无效运行(字符串…参数)引发异常{
试一试{
RestTemplate RestTemplate=新RestTemplate();
HttpHeaders HttpHeaders=新的HttpHeaders();
httpHeaders=this.createHeaders();
反应性反应;
response=restemplate.exchange(“”,HttpMethod.GET,新的HttpEntity(httpHeaders),String.class);
log.info(response.toString());
}
捕获(例外e)
{
系统输出打印项次(“例外”+e);
}
}
私有HttpHeaders createHeaders(){
HttpHeaders=新的HttpHeaders(){
{
集合(“授权”、“3ee140”);
}
};
返回标题;
}
}是否应该是requestHeaders.get(“身份验证”).get(0)?为什么?我设置了一个带有“Authorization”的头,我希望在服务器端得到相同的头
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
try{
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders = this.createHeaders();
ResponseEntity<String> response;
response = restTemplate.exchange("<url>",HttpMethod.GET,new HttpEntity<Object>(httpHeaders),String.class);
log.info(response.toString());
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
private HttpHeaders createHeaders(){
HttpHeaders headers = new HttpHeaders(){
{
set( "Authorization", "3ee140");
}
};
return headers;
}