How to validate RestTemplate response?
Asked Answered
V

2

8

Spring supports annotation based validation at the controller level.

(1) Is it necessary to do such validations also at the RestTemplate level for responses from REST calls?

If the answer is Yes: (2) Will there be support for that at the RestTemplate to validate responses from rest calls sometime in the future?

If the answer is No: (3) why?

Vise answered 28/8, 2017 at 17:10 Comment(1)
Clarification: My question is NOT regarding validating a request. My question is on "validating a response from another service".Vise
K
3

It is 2020 now and I still do not see the requested feature in place.

The @Valid is nice to automatically validate e.g. a posted RequestBody.

But for the validation of the body of a ResponseEntity fetched via RestTemplate, I do not see any fancy equivalent.

So the only option I know, is to do it on your own taken from here. Input is the class of your RequestEntitys body. input is the body itself.

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Input>> violations = validator.validate(input);
if (!violations.isEmpty()) {
  throw new ConstraintViolationException(violations);
}

So to answer your question:

  1. Yes I would validate the response!
  2. future (2020) has not brought the feature you and I miss
  3. For the reason why this is missing, I also have no answer.
Knowledgeable answered 20/5, 2020 at 10:3 Comment(0)
P
0

For me, the questions are quite big. :). As my understanding, you would like to ask the validation in REST service that Spring can support.

1. Is it necessary to do such validations also at the RestTemplate level for responses from REST calls?

Actually, it depends on your apps or your business. You can do at Controller or you can do in Service Level or even you can do your custom validation. For me, no one forces you to do anything.

However, as my experience, we should do the validation for sure. So my answer here is YES.

2. Will there be support for that at the RestTemplate to validate responses from rest calls sometime in the future?

I assume that you would like the detail of the validation?! Right? Spring supports many things to do the validation. For the simple way, you can use PathVariable or RequestParameter.For example:

@GetMapping("/test/{name}")
    private String test(@PathVariable(value = "name", required = true) String name){
        //...
    }

Spring will validate all requests, and respond with 400 Bad Request when the required parameter is missing or has a wrong type...

Spring also supports the JSR 303 Bean Validation: http://beanvalidation.org/1.0/spec/ For example in here:

public class MessageBean {
    @NotNull
    private String title;
    @NotNull
    private String message;
 
    // getters/setters/etc
}

Or you would like to do the Custom User Response like:

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleException(MethodArgumentNotValidException exception) {
    //....
    return ErrorResponse.builder().message(errorMsg).build();
}

Some more details in here: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-validation

So, it depends on the business which requires us to do the validation at any level.

3. If the answer is No: (3) why?

No need to answer this. :)

Hope that helps

Pledge answered 28/8, 2017 at 23:44 Comment(2)
Thank you for your answer but this is not what I was looking for. Probably my question was not clear enough. I am okay with validating a "request" at any layer depending on technical business needs. My question is NOT regarding validating a request. My question is on "validating a response from another service".Vise
I think you didn't understand the question mate.Bradybradycardia

© 2022 - 2024 — McMap. All rights reserved.