3

in my web application I have many places where I need to retrieve current logged account (from session).

Account account = getAccountFromSession(session);

This piece of code is repeated many many times. I wonder if this is possible to make my request look something like this:

@RequestMapping(value="/something", method=RequestMethod.POST)
public String handleSomething(
        @RequestParam String someParam,
        @Account Account account) { ... }

where @Account is my custom annotation.

How can I tell spring to process this annotation?

1 Answer 1

4

Use HandlerMethodArgumentResolver (or WebArgumentResolver for versions below 3.1).

To enable your argument resolver:

  • If you use @EnableWebMvc - make your @Configuration implement WebMvcConfigurer and override addArgumentResolvers()
  • If you use <mvc:annotation-driven> - use argument-resolvers attribute
  • For older versions you may need to declare AnnotationMethodHandlerAdapter manually and set its customArgumentResolvers
Sign up to request clarification or add additional context in comments.

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.