简体   繁体   English

如何对 Mediatypes Rest 端点进行 restTemplate.getForObject 调用

[英]How to make restTemplate.getForObject call for Mediatypes Rest endpoint

I have a Java endpoint like this我有一个像这样的 Java 端点

 @RequestMapping(method = RequestMethod.GET, value = "/{test}", produces = "application/app+json;version=1")
public ResponseEntity<List<Entity>> getEntity(@PathVariable Long test) {
        
        return ........
    }

Now am making call to this rest endpoint using restTempate via URIBuilder现在我通过 URIBuilder 使用 restTempate 调用这个 rest 端点

String url = UriComponentsBuilder.fromHttpUrl(this.URL)
                .path(API_URL)
                .path("/{test}")
                .buildAndExpand(test).toString(); //How to add Headers??

 return Arrays.asList(restTemplate.getForObject(url, Entity[].class));

I am tryng to add the header on the rest endpoint call but not sure what is the right place to add it.我正在尝试在 rest 端点调用上添加 header,但不确定添加它的正确位置。 Or is there any other right way of doing it?或者还有其他正确的方法吗? please suggest请建议

It's not possible to send custom headers by calling the getForObject method.无法通过调用getForObject方法发送自定义标头。 Instead, you can use the exchange method.相反,您可以使用exchange方法。

The code would be something like this:代码将是这样的:

HttpHeaders headers = new HttpHeaders();
headers.add("HEADER", "VALUE");

HttpEntity<String> entity = new HttpEntity<>(null, headers);

restTemplate.exchange(url, HttpMethod.GET, entity, Entity[].class);

RestTemplate.getForObject() method does not support setting headers. RestTemplate.getForObject()方法不支持设置标题。 The solution is to use the RestTemplate.exchange() method.解决方案是使用RestTemplate.exchange()方法。

You can add headers:您可以添加标题:

String url = UriComponentsBuilder.fromHttpUrl(this.URL)
                .path(API_URL)
                .path("/{test}")
                .buildAndExpand(test).toString();

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("header-key", "header-value");
...

HttpEntity<String> httpEntity = new HttpEntity<>(null, httpHeaders);

ResponseEntity<Entity[]> response = restTemplate.exchange(
    url, HttpMethod.GET, httpEntity, Entity[].class);

return Arrays.asList(response);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 restTemplate.getForObject上的HttpClientErrorException - HttpClientErrorException on restTemplate.getForObject 调用restTemplate.getForObject时如何处理未找到数据 - How to handle no data found when calling restTemplate.getForObject 没有获得通过RestTemplate.getForObject传递的标头 - not getting headers passed with RestTemplate.getForObject Spring Boot 微服务 - 将授权标头添加到 restTemplate.getForObject - Spring Boot Microservices - Add authorization header to restTemplate.getForObject restTemplate.getForObject(“URL”,Object [] .class)可以返回NULL吗? - Can restTemplate.getForObject(“URL”, Object[].class) return NULL? Spring restTemplate.getForObject()的JSON转换错误 - JSON conversion error from Spring restTemplate.getForObject() 在 Android 中使用 restTemplate.getForObject 反序列化 JSON object 的正确方法 - Proper way to use restTemplate.getForObject to deserialize JSON object in Android 如何通过受Keycloak保护的Spring restTemplate调用Rest端点 - How to call rest endpoint via Spring restTemplate that is protected by Keycloak 使用 RestTemplate 调用外部 REST 端点时出错 - Error on external REST endpoint call using RestTemplate 如何使用jmockit模拟RestTemplate getForObject方法? - How to mock RestTemplate getForObject method using jmockit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM