配置 Spring RestTemplate
Configuring Spring RestTemplate
调用REST服务有以下代码
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
String plainCreds = "test:test2";
byte[] plainCredsBytes = plainCreds.getBytes();
String base64Creds = DatatypeConverter.printBase64Binary(plainCredsBytes);
headers.add("Authorization", "Basic " + base64Creds);
headers.add("Content-type","application/x-www-form-urlencoded;charset=utf-8");
headers.set("Accept", MediaType.APPLICATION_XML_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl)
.queryParam("id", "id1234");
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(
builder.build().encode().toUri(),
HttpMethod.GET, entity, String.class);
对此有以下疑惑-
- 我可以使用工厂模式来获取 RestTemplate 而不是使用新的。它的优点和缺点是什么
- 目前正在将凭据添加到上述代码中的 headers 中。有没有更好的方法来实现它(配置示例或其他适合生产代码的方式)。
谢谢
RestTemplate的一般使用模式是你按照你想要的方式配置一个然后重用这贯穿您的所有应用程序。它是 thread safe 但创建起来可能很昂贵,因此创建尽可能少(最好只创建一个)并重复使用它们是您应该做的。
有一些方法可以配置 RestTemplate 以自动为所有请求添加基本身份验证,但 IMO 太复杂了,不值得 - 你需要搞砸有点 Http Components and create your own request factory 所以我认为最简单的解决方案是将手动步骤分解为助手 class.
--
public class RestTemplateUtils {
public static final RestTemplate template;
static {
// Init the RestTemplate here
template = new RestTemplate();
}
/**
* Add basic authentication to some {@link HttpHeaders}.
* @param headers The headers to add authentication to
* @param username The username
* @param password The password
*/
public static HttpHeaders addBasicAuth(HttpHeaders headers, String username, String password) {
String plainCreds = username + ":" + password;
byte[] plainCredsBytes = plainCreds.getBytes();
String base64Creds = DatatypeConverter.printBase64Binary(plainCredsBytes);
headers.add("Authorization", "Basic " + base64Creds);
return headers;
}
}
您的代码现在变成:
HttpHeaders headers = RestTemplateUtils.addBasicAuth(new HttpHeaders(),
"test", "test");
headers.add("Content-type","application/x-www-form-urlencoded;charset=utf-8");
headers.set("Accept", MediaType.APPLICATION_XML_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl)
.queryParam("id", "id1234");
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = RestTemplateUtils.template.exchange(
builder.build().encode().toUri(),
HttpMethod.GET, entity, String.class);
尽管我建议添加更多辅助方法来创建实体并向其添加任何其他标准 headers。
调用REST服务有以下代码
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
String plainCreds = "test:test2";
byte[] plainCredsBytes = plainCreds.getBytes();
String base64Creds = DatatypeConverter.printBase64Binary(plainCredsBytes);
headers.add("Authorization", "Basic " + base64Creds);
headers.add("Content-type","application/x-www-form-urlencoded;charset=utf-8");
headers.set("Accept", MediaType.APPLICATION_XML_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl)
.queryParam("id", "id1234");
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(
builder.build().encode().toUri(),
HttpMethod.GET, entity, String.class);
对此有以下疑惑-
- 我可以使用工厂模式来获取 RestTemplate 而不是使用新的。它的优点和缺点是什么
- 目前正在将凭据添加到上述代码中的 headers 中。有没有更好的方法来实现它(配置示例或其他适合生产代码的方式)。
谢谢
RestTemplate的一般使用模式是你按照你想要的方式配置一个然后重用这贯穿您的所有应用程序。它是 thread safe 但创建起来可能很昂贵,因此创建尽可能少(最好只创建一个)并重复使用它们是您应该做的。
有一些方法可以配置 RestTemplate 以自动为所有请求添加基本身份验证,但 IMO 太复杂了,不值得 - 你需要搞砸有点 Http Components and create your own request factory 所以我认为最简单的解决方案是将手动步骤分解为助手 class.
--
public class RestTemplateUtils {
public static final RestTemplate template;
static {
// Init the RestTemplate here
template = new RestTemplate();
}
/**
* Add basic authentication to some {@link HttpHeaders}.
* @param headers The headers to add authentication to
* @param username The username
* @param password The password
*/
public static HttpHeaders addBasicAuth(HttpHeaders headers, String username, String password) {
String plainCreds = username + ":" + password;
byte[] plainCredsBytes = plainCreds.getBytes();
String base64Creds = DatatypeConverter.printBase64Binary(plainCredsBytes);
headers.add("Authorization", "Basic " + base64Creds);
return headers;
}
}
您的代码现在变成:
HttpHeaders headers = RestTemplateUtils.addBasicAuth(new HttpHeaders(),
"test", "test");
headers.add("Content-type","application/x-www-form-urlencoded;charset=utf-8");
headers.set("Accept", MediaType.APPLICATION_XML_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl)
.queryParam("id", "id1234");
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = RestTemplateUtils.template.exchange(
builder.build().encode().toUri(),
HttpMethod.GET, entity, String.class);
尽管我建议添加更多辅助方法来创建实体并向其添加任何其他标准 headers。