简体   繁体   中英

Hystrix: how to perform @Requestparam and bean validation?

I am trying to perform some parameters validations on my rest controller, by using the JSR-303 bean validation. However, all validations are ignored when I annotate my method with the @HystrixCommand annotation.

For example :

    @RestController
    @Validated
    public class TestController {

        //This method ignore all validations :(, even if age < 10
        @HystrixCommand(fallbackMethod = "fallback")
        @RequestMapping(value = "/notok", method = RequestMethod.GET)
        ResponseEntity<String> methodNotOk(
                HttpServletRequest request,
                @Min(10) @RequestParam(name = "age") final int age
        ) {
            return ResponseEntity.ok("Age is " + age);
        }

        //This method will raised an error if age < 10, because of the age validation.
        //This is the expected behavior
        @RequestMapping(value = "/ok", method = RequestMethod.GET)
        ResponseEntity<String> methodOk(
                HttpServletRequest request,
                @Min(10) @RequestParam(name = "age") final int age
        ) {
            return ResponseEntity.ok("Age is " + age);
        }

        //Hystrix fallback
        private ResponseEntity<String> fallback(HttpServletRequest request, @RequestParam("age") final int age, Throwable e) {
            return ResponseEntity.badRequest().body("error");
        }

    }

For the methodNotOk , the @Min annotation is totally ignored, so the server can respond Age is 5 , which is incorrect for this test case.

But, if I remove the @HystrixCommand , all go like clockwork.

So the question is how can I validate some method parameters when using @HystrixCommand ?

Thanks for the help

I have the same problem and I have not found anything on the internet.

I think the cause is the order that the Aspects are loaded (HystrixCommandAspect create a proxy of your Bean before that Validator is loaded), but I can not find a way to change it using the Java configuration of Spring

My last resort has been to move the logic to another Bean with @HystrixCommand, and then use the delegate pattern in the Controller

@RestController
@Validated
public class TestController {

    @Autowired
    private TestHystrixController delegate;

    @RequestMapping(value = "/notok", method = RequestMethod.GET)
    ResponseEntity<String> methodNotOk(HttpServletRequest request,
         @Min(10) @RequestParam(name = "age") final int age) {
        return delegate.methodNotOk(request, age);
    }
}

@Controller
class TestHystrixController {

    @HystrixCommand(fallbackMethod = "fallback")
    ResponseEntity<String> methodNotOk(HttpServletRequest request,
         final int age) {
        return ResponseEntity.ok("Age is " + age);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM