0

I have a custom dispatcher servlet which extends the default DispatcherServlet to do some validation for all requests.

Since I will get all the parameters from a request(getInputStream()->Map) to do some validation, I want to pass the params to controller or add the params to the context where I can get them again from the cotroller.

Now I just put all the params to a global Map, but I wonder if there are some simple ways.

public class CustomDispatcherServlet extends DispatcherServlet {

private static final long serialVersionUID = 7250693017796274410L;

@Override
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {

    doFilter(request, response);
    super.doDispatch(request, response);
}
...

private void doFilter(HttpServletRequest request, HttpServletResponse response) {
    WLNResponse<String> error = null;

    try {
        boolean isSignValid = checkSignValidity(request);
        ...

private boolean checkSignValidity(HttpServletRequest request) throws IOException {
      // pass this params to controller or somewhere I can get from controller      
      Map<String, Object> params = WebUtils.readParams(request); 
      ...

2 Answers 2

1

The way I would go at validating params in the controller itself. for instance

@Controller
public ControllerClass
{
  @RequestMapping(value = "/someurl", method = RequestMethod.GET, params = {
    "requestParam"})
  public void someMethod(@RequestParam(value = "requestParam") String requestParam)
  {
    System.out.println("This is the value of the RequestParam requestParam " + requestParam);
  }
}

This way you can do your validation within the controller.

The only thing this doesn't solve for is if the request being made is not resolved to a valid controller. For that I would use the annotation @controllerAdvice.

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

2 Comments

I am building a RESTful Server, I will check all the request first, and the validation logic for all requests is the same. So I want to do this validation before the controller
Take a look at Spring's DispatcherServletWebRequest class and then implementing Spring's Validator interface
0

Currently, I simply use the request.setAttribute() to put params to the attributes and get it from the controller.....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.