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

  • SEARCH
  • Home
  • 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 7162301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:42:44+00:00 2026-05-28T13:42:44+00:00

I have a command object: public class Job { private String jobType; private String

  • 0

I have a command object:

public class Job {
    private String jobType;
    private String location;
}

Which is bound by spring-mvc:

@RequestMapping("/foo")
public String doSomethingWithJob(Job job) {
   ...
}

Which works fine for http://example.com/foo?jobType=permanent&location=Stockholm. But now I need to make it work for the following url instead:

http://example.com/foo?jt=permanent&loc=Stockholm

Obviously, I don’t want to change my command object, because the field names have to remain long (as they are used in the code). How can I customize that? Is there an option to do something like this:

public class Job {
    @RequestParam("jt")
    private String jobType;
    @RequestParam("loc")
    private String location;
}

This doesn’t work (@RequestParam can’t be applied to fields).

The thing I’m thinking about is a custom message converter similar to FormHttpMessageConverter and read a custom annotation on the target object

  • 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-05-28T13:42:45+00:00Added an answer on May 28, 2026 at 1:42 pm

    Here’s what I got working:

    First, a parameter resolver:

    /**
     * This resolver handles command objects annotated with @SupportsAnnotationParameterResolution
     * that are passed as parameters to controller methods.
     * 
     * It parses @CommandPerameter annotations on command objects to
     * populate the Binder with the appropriate values (that is, the filed names
     * corresponding to the GET parameters)
     * 
     * In order to achieve this, small pieces of code are copied from spring-mvc
     * classes (indicated in-place). The alternative to the copied lines would be to
     * have a decorator around the Binder, but that would be more tedious, and still
     * some methods would need to be copied.
     * 
     * @author bozho
     * 
     */
    public class AnnotationServletModelAttributeResolver extends ServletModelAttributeMethodProcessor {
    
        /**
         * A map caching annotation definitions of command objects (@CommandParameter-to-fieldname mappings)
         */
        private ConcurrentMap<Class<?>, Map<String, String>> definitionsCache = Maps.newConcurrentMap();
    
        public AnnotationServletModelAttributeResolver(boolean annotationNotRequired) {
            super(annotationNotRequired);
        }
    
        @Override
        public boolean supportsParameter(MethodParameter parameter) {
            if (parameter.getParameterType().isAnnotationPresent(SupportsAnnotationParameterResolution.class)) {
                return true;
            }
            return false;
        }
    
        @Override
        protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
            ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
            ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
            bind(servletRequest, servletBinder);
        }
    
        @SuppressWarnings("unchecked")
        public void bind(ServletRequest request, ServletRequestDataBinder binder) {
            Map<String, ?> propertyValues = parsePropertyValues(request, binder);
            MutablePropertyValues mpvs = new MutablePropertyValues(propertyValues);
            MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
            if (multipartRequest != null) {
                bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
            }
    
            // two lines copied from ExtendedServletRequestDataBinder
            String attr = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
            mpvs.addPropertyValues((Map<String, String>) request.getAttribute(attr));
            binder.bind(mpvs);
        }
    
        private Map<String, ?> parsePropertyValues(ServletRequest request, ServletRequestDataBinder binder) {
    
            // similar to WebUtils.getParametersStartingWith(..) (prefixes not supported)
            Map<String, Object> params = Maps.newTreeMap();
            Assert.notNull(request, "Request must not be null");
            Enumeration<?> paramNames = request.getParameterNames();
            Map<String, String> parameterMappings = getParameterMappings(binder);
            while (paramNames != null && paramNames.hasMoreElements()) {
                String paramName = (String) paramNames.nextElement();
                String[] values = request.getParameterValues(paramName);
    
                String fieldName = parameterMappings.get(paramName);
                // no annotation exists, use the default - the param name=field name
                if (fieldName == null) {
                    fieldName = paramName;
                }
    
                if (values == null || values.length == 0) {
                    // Do nothing, no values found at all.
                } else if (values.length > 1) {
                    params.put(fieldName, values);
                } else {
                    params.put(fieldName, values[0]);
                }
            }
    
            return params;
        }
    
        /**
         * Gets a mapping between request parameter names and field names.
         * If no annotation is specified, no entry is added
         * @return
         */
        private Map<String, String> getParameterMappings(ServletRequestDataBinder binder) {
            Class<?> targetClass = binder.getTarget().getClass();
            Map<String, String> map = definitionsCache.get(targetClass);
            if (map == null) {
                Field[] fields = targetClass.getDeclaredFields();
                map = Maps.newHashMapWithExpectedSize(fields.length);
                for (Field field : fields) {
                    CommandParameter annotation = field.getAnnotation(CommandParameter.class);
                    if (annotation != null && !annotation.value().isEmpty()) {
                        map.put(annotation.value(), field.getName());
                    }
                }
                definitionsCache.putIfAbsent(targetClass, map);
                return map;
            } else {
                return map;
            }
        }
    
        /**
         * Copied from WebDataBinder.
         * 
         * @param multipartFiles
         * @param mpvs
         */
        protected void bindMultipart(Map<String, List<MultipartFile>> multipartFiles, MutablePropertyValues mpvs) {
            for (Map.Entry<String, List<MultipartFile>> entry : multipartFiles.entrySet()) {
                String key = entry.getKey();
                List<MultipartFile> values = entry.getValue();
                if (values.size() == 1) {
                    MultipartFile value = values.get(0);
                    if (!value.isEmpty()) {
                        mpvs.add(key, value);
                    }
                } else {
                    mpvs.add(key, values);
                }
            }
        }
    }
    

    And then registering the parameter resolver using a post-processor. It should be registered as a <bean>:

    /**
     * Post-processor to be used if any modifications to the handler adapter need to be made
     * 
     * @author bozho
     *
     */
    public class AnnotationHandlerMappingPostProcessor implements BeanPostProcessor {
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String arg1)
                throws BeansException {
            return bean;
        }
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String arg1)
                throws BeansException {
            if (bean instanceof RequestMappingHandlerAdapter) {
                RequestMappingHandlerAdapter adapter = (RequestMappingHandlerAdapter) bean;
                List<HandlerMethodArgumentResolver> resolvers = adapter.getCustomArgumentResolvers();
                if (resolvers == null) {
                    resolvers = Lists.newArrayList();
                }
                resolvers.add(new AnnotationServletModelAttributeResolver(false));
                adapter.setCustomArgumentResolvers(resolvers);
            }
    
            return bean;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a command object associated with a spring form controller: public class PluginInstance
I have a Grails command object that contains an emailAddresses field, e.g. public class
I have an interface class public interface AsyncTaskExecuteCommand { public Object executeCommand(String jsonLocation) throws
I have a class Account public class Account { private int id; private String
Let's say I have an object who's class definition looks like: class Command: foo
I have .NET assembly with one public class and several private classes. I am
At the moment I have the following Command class: Public Class SubscribeCommand Implements ICommand
Let's say I have a class public class foo { public string FooId {
I have a populated SqlCommand object containing the command text and parameters of various
I have a command line program, which outputs logging to the screen. I want

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.