有没有办法将Spring RestTemplate与JavaBean验证API集成。 ?
我知道弹簧控制器有一个集成。您可以将@Valid
放在请求正文参数上,如果Employee无效,您将获得MethodArgumentNotValidException
异常。你可以在异常处理程序类中使用它。
@PostMapping(value = "/add", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<String> addEmployee(
@RequestBody @Valid Employee emp) {
//...
}
但我想要的是类似的方法来验证来自spring restTemplate的响应 当我这样打电话时 - 我希望从春天得到同样的(或可能是其他的)例外。
Employee emp = template.exchange("http:///someUrl", HttpMethod.GET, null);
我知道我可以像这样注入验证器并在响应时调用validator.validate(..)。
@Autowired
private Validator validator;
但我不想每次都手动这样做。
答案 0 :(得分:3)
您可以将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
bean。
完整样本:
@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());
};
}
}