我正在尝试向第三方GET
发送api
请求。我正在尝试设置两个键,但是由于clientHttpRequestFactory
无法解析此方法而无法设置。阅读RestTemplate
的文档,看来我做得正确。
@Configuration
public class RequestHeaderConfig {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
restTemplate.setInterceptors(Collections.singletonList(new RequestHeader()));
return restTemplate;
}
}
public class RequestHeader implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
HttpHeaders headers = request.getHeaders();
headers.add("my-key","068a99aksdjfhk9uug67");
headers.add("subscription-Key","068a999uug67");
return execution.execute(request, body);
}
}
compile("io.springfox:springfox-swagger2:${springfoxSwaggerVersion}")
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.20'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web-services', version: springBootVersion
compile group: 'org.springframework', name: 'spring-web', version: '5.1.4.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: springBootVersion
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
testCompile group: 'junit', name: 'junit', version: '4.12'
}
@RequestMapping(value = "/api/T1/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@ApiOperation(value = "This endpoint will get a text from one language to another")
public ResponseEntity<String> getTranslation() {
log.info("getTranslation : {} ");
return new ResponseEntity<String>("TEST TEST TEST", HttpStatus.ACCEPTED);
}
答案 0 :(得分:0)
您尚未在config类中定义clientHttpRequestFactory
方法。如果您不需要,则只需将其从构造函数调用中删除
@Configuration
public class RequestHeaderConfig {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList(new RequestHeader()));
return restTemplate;
}
}