Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8564801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:16:49+00:00 2026-06-11T17:16:49+00:00

I wanted to create custom controller method argument annotation. Following this question How to

  • 0

I wanted to create custom controller method argument annotation.

Following this question How to pass a session attribute as method argument (parameter) with Spring MVC and following @Bozho advice I have something like this:

my resolver

public class SessionAttributeAnnotationResolver implements WebArgumentResolver {

    public Object resolveArgument(MethodParameter parameter,
            NativeWebRequest request) throws Exception {
        System.out.println("I am here");
        Annotation[] parameterAnnotations = parameter.getParameterAnnotations();
        Class<?> parameterType = parameter.getParameterType();

        for (Annotation parameterAnnotation : parameterAnnotations) {
            if (SessionAttribute.class.isInstance(parameterAnnotation)) {
                SessionAttribute sessionAttribute = (SessionAttribute) parameterAnnotation;
                String parameterName = sessionAttribute.value();
                boolean required = sessionAttribute.required();
                HttpServletRequest httprequest = (HttpServletRequest) request
                        .getNativeRequest();
                HttpSession session = httprequest.getSession(false);
                Object result = null;
                if (session != null) {
                    result = session.getAttribute(parameterName);
                }
                if (result == null && required && session == null)
                    raiseSessionRequiredException(parameterName, parameterType);
                if (result == null && required)
                    raiseMissingParameterException(parameterName, parameterType);
                return result;
            }
        }
        return WebArgumentResolver.UNRESOLVED;
    }

    protected void raiseMissingParameterException(String paramName,
            Class<?> paramType) throws Exception {
        throw new IllegalStateException("Missing parameter '" + paramName
                + "' of type [" + paramType.getName() + "]");
    }

    protected void raiseSessionRequiredException(String paramName,
            Class<?> paramType) throws Exception {
        throw new HttpSessionRequiredException(
                "No HttpSession found for resolving parameter '" + paramName
                        + "' of type [" + paramType.getName() + "]");
    }
}

the annotation

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface SessionAttribute {
    String value();
    boolean required() default true;
}

simple controller to test everything

@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping(method= RequestMethod.GET)
    public String t(@SessionAttribute("userEntity") UserEntity e2,Model model,HttpServletRequest req){
        System.out.println(req.getSession().getId());
        UserEntity e=(UserEntity) req.getSession().getAttribute("userEntity");
        System.out.println(e.getName());
        System.out.println(e2.getName());
        return "login";
    }
}

and finally, Spring configuration

<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">     
    <beans:property name="customArgumentResolver" ref="sessionAttributeAnnotationResolver"/>
</beans:bean>
<beans:bean id="sessionAttributeAnnotationResolver" class="pl.meble.taboret.utils.SessionAttributeAnnotationResolver"/>  

now, everything seems in order to me, but there is probably some silly mistake that I done, because when controller is executed, I am getting

F0B282C93B74F8FA3F21A51F46D4D4D5
username
null
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:16:50+00:00Added an answer on June 11, 2026 at 5:16 pm

    With Spring 3.1.0 the ArgumentResolver has now changed to HandlerMethodArgumentResolver – prior to that it used to be WebArgumentResolver – a related answer is here

    Once you have written a new HandlerMethodArgumentResolver which is not very different from your current implementation you can register it this way:

    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean id="sessionAttributeAnnotationResolver" class="..SessionAttributeAnnotationResolver ">
            </bean>            
        </mvc:argument-resolvers>
    </mvc:annotation-driven>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to create a custom combo box like this (as in MS Word),
I wanted to create a custom JUnit annotation, something similar to expected tag in
I wanted to create Custom Dialog like the following image which can be called
I wanted to create smth similar to this one Kansas county map where user
I wanted to create a very simple method that switches between views in a
I've always wanted to create custom components in Java, or customize existing ones, but
I wanted to create a custom bottom button bar layout, I've created a xml
I have decided that I wanted to create some custom dialog classes that could
I am a fairly new resharper user. I wanted to create a custom pattern
I created a Vaadin Project, then wanted to create a Custom composite and display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.