您可以繼承RestTemplate
的子類,並在從原始數據提取它之後立即驗證響應。
例子:
public class ValidatableRestTemplate extends RestTemplate {
private final Validator validator;
public ValidatableRestTemplate(Validator validator) {
this.validator = validator;
}
@Override
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws RestClientException {
final T response = super.doExecute(url, method, requestCallback, responseExtractor);
Object body;
if (response instanceof ResponseEntity<?>) {
body = ((ResponseEntity) response) .getBody();
} else {
body = response;
}
final Set<ConstraintViolation<Object>> violations = validator.validate(body);
if (violations.isEmpty()) {
return response;
}
throw new ConstraintViolationException("Invalid response", violations);
}
}
的使用則是相當簡單的,只是定義你的子類爲RestTemplate
豆。
全樣本:
@SpringBootApplication
public class So45333587Application {
public static void main(String[] args) { SpringApplication.run(So45333587Application.class, args); }
@Bean
RestTemplate restTemplate(Validator validator) { return new ValidatableRestTemplate(validator); }
public static class ValidatableRestTemplate extends RestTemplate {
private final Validator validator;
public ValidatableRestTemplate(Validator validator) { this.validator = validator; }
@Override
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws RestClientException {
final T response = super.doExecute(url, method, requestCallback, responseExtractor);
Object body;
if (response instanceof ResponseEntity<?>) {
body = ((ResponseEntity) response).getBody();
} else {
body = response;
}
final Set<ConstraintViolation<Object>> violations = validator.validate(body);
if (violations.isEmpty()) {
return response;
}
throw new ConstraintViolationException("Invalid response", violations);
}
}
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public static class Post {
@Min(2) // change to '1' and constraint violation will disappear
private Long id;
private Long userId;
private String title;
private String body;
@Override
public String toString() {
return String.format("Post{id=%d, userId=%d, title='%s', body='%s'}", id, userId, title, body);
}
}
@Bean
CommandLineRunner startup(RestTemplate restTemplate) {
return args -> {
final ResponseEntity<Post> entity = restTemplate.exchange("https://jsonplaceholder.typicode.com/posts/1", HttpMethod.GET, null, Post.class);
System.out.println(entity.getBody());
};
}
}
什麼是你需要做的這個你的使用情況?從技術上講,我不能真正想到一個你不想使用驗證器對象的地方,除非它是某種代理服務......可以手動驗證它,在某些情況下可以更有用。 – Chad
感謝您的回覆。我只是想,也許春天有一些解決方案。我有很多res的調用,我不想在restTemplate返回的每個對象上調用validate方法。 – user1321466