Announcement Announcement Module
Collapse
No announcement yet.
@RequestBody parameter with type of Object receives a SimpleSessionStatus object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @RequestBody parameter with type of Object receives a SimpleSessionStatus object

    Hi all,

    If I annotate an Object typed parameter with @RequestBody I get the SimpleSessionState object instead of the properly parsed body of the request.

    Example:
    Code:
    	public List<NetResponse> requester(HttpServletRequest request, HttpServletResponse response, Locale locale,
    			@RequestBody Object bodyData) {
    if (bodyData instanceof Map){
       processMap((Map)bodyData);
    }
    if (bodyData instanceof Collection){
       processCollection((Collection)bodyData);
    }
    
    }
    I've traced the problem to the following line method in the org.springframework.web.method.support.InvocableHa ndlerMethod class.
    Code:
    args[i] = resolveProvidedArgument(parameter, providedArgs);
    			if (args[i] != null) {
    continue;
    }
    Code for actual Spring framework method.
    Code:
    private Object[] getMethodArgumentValues(NativeWebRequest request,*
    			ModelAndViewContainer mavContainer,*
    			Object... providedArgs) throws Exception {
    		MethodParameter[] parameters = getMethodParameters();
    		Object[] args = new Object[parameters.length];
    		for (int i = 0; i < parameters.length; i++) {
    			MethodParameter parameter = parameters[i];
    			parameter.initParameterNameDiscovery(this.parameterNameDiscoverer);
    			GenericTypeResolver.resolveParameterType(parameter, getBean().getClass());
    
    
    
    
    			args[i] = resolveProvidedArgument(parameter, providedArgs);
    			if (args[i] != null) {
    				continue;
    			}
    			if (this.argumentResolvers.supportsParameter(parameter)) {
    				args[i] = this.argumentResolvers.resolveArgument(parameter, mavContainer, request, dataBinderFactory);
    			}
    			else {
    				throw new IllegalStateException("Cannot resolve argument index=" + parameter.getParameterIndex() + ""
    						+ ", name=" + parameter.getParameterName() + ", type=" + parameter.getParameterType()
    						+ " in method " + toString());
    			}
    		}
    		return args;
    	}
    Because the parameter is of type Object it matches any providedArgs and the one that's provided in this case is the SimpleSessionStatus object.

    Shouldn't the parameter checking look at the Annotations first so that an Object typed parameter can be used?



    Thanks
    Robert Brown

  • #2
    What are you trying to do here, are you just trying to pass a request parameter into your method ?

    Comment


    • #3
      I'm trying to pass the request body to the method, and have that body parsed by the MessageConverter (in my case a JSON converter) into an object. In this case it is expecting that object to either be a Map or a Collection so I can't specify a particular object type in the parameters. The body of the request will contain a json formatted object. (an Ext.direct request from the ExtJS framework in particular). That object is either a single request or an array of requests. If it's a single request the JSON converter will convert it into a Map, if it's an array of request it becomes a list of maps. Using Ext.direct you can't tell it to use a different url for single vs batch requests so it either sends a single object or a list of objects and your servlet has to work it out.

      The root of the problem though is that it checks the Type of the parameter first instead of letting the annotation take precedence so a parameter of type object will always match the SimpleSessionStatus object passed into the function.

      Note, it would probably do the same thing if I specified a @RequestParam annotation on an Object typed parameter.

      I think it's a bug introduced in 3.x somewhere because the code I'm running is from a library that has an example that works running on 2.5.

      Thanks
      Robert Brown

      Comment


      • #4
        This should not happen in 3.1.0.RELEASE. Make sure it's what you have rather than a milestone or a release candidate. In 3.1.0.RELEASE the SimpleSessionStatus gets resolved through an argument resolver (rather than as a provided arg).

        Comment


        • #5
          Ho Rossen,

          I was using the M2 release. I'll get the latest and and retest.

          Thanks
          Bob

          Comment

          Working...
          X