【发布时间】:2020-04-18 05:56:48
【问题描述】:
我需要一种创建动态/通用休息客户端的方法。我的 Spring Boot 应用程序需要连接到许多第三方客户端,它们都有不同的请求正文、响应正文,有些需要特殊的标头,而有些需要特殊的授权/身份验证(例如 Basic Auth、JWT、HMAC 等)。
到目前为止,我已经设法提出了以下客户端
public class GenericRestClient<T, V> {
private RestTemplate restTemplate = new RestTemplate();
public V execute(RequestDetails requestDetails, T data, ResponseErrorHandler errorHandler,
Class<V> genericClass) throws ResourceAccessException, Exception {
restTemplate.setErrorHandler(errorHandler);
HttpHeaders headers = new HttpHeaders();
HttpEntity<T> entity = new HttpEntity<T>(data, headers);
ResponseEntity<V> response = restTemplate.exchange(requestDetails.getUrl(), requestDetails.getRequestType(),
entity, genericClass);
return response.getBody();
}
}
但我的问题是。当它们都有不同的要求时,现在有没有办法生成所有必要的标头、身份验证和授权?我该怎么做?
有没有办法将 java 代码像脚本(例如 JTX)一样存储在数据库中并使用它们,或者有没有更好的方法来满足我的需求?
我希望即使有新客户来,也不需要进行进一步的编码。
【问题讨论】:
标签: java spring spring-boot