3

Is there any way through which I can validate function parameters of type String using @Valid annotation ?

I have been trying to do that but not able to do so. Here is my code:-

@Controller public class GameController { @Autowired private HeaderValidator headerValidator; @InitBinder private void initBinder(WebDataBinder binder) { binder.setValidator(new CompoundValidator(headerValidator)); } @RequestMapping(value = "game", method = RequestMethod.GET) public @ResponseBody Game getGame( @Valid @RequestHeader(value = "header", required = true) String header) { return new Game(1, "Baddy"); } } 
2
  • Nope. @Valid works for model attributes not for header values. You would have to do manual validation for that. Commented Sep 9, 2015 at 10:12
  • is there no other way to validate header ? Commented Sep 10, 2015 at 18:45

2 Answers 2

2

Since I don't have enough reputation to add my comments....writing here. In addition to what @M.Deinum commented,there is open JIRA for the same, and I guess the fix is expected in 4.3.

https://jira.spring.io/si/jira.issueviews:issue-html/SPR-6380/SPR-6380.html

Sign up to request clarification or add additional context in comments.

Comments

0

Not sure how to do it using annotations, but any operations on headers can be easily done using Filters or Interceptors. Here is one example of how to use interceptors in spring Mykong.com

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.