4

For some reason my Tomcat/Spring configuration doesn't automatically decode the GET parameters ( I assume it should be done automatically ).

Here is my setup:

@RestController
public class MyController {
    @RequestMapping(value = "/do-something", method = RequestMethod.GET)
    public String doSomething(RequestPojo req) {
        // req.getPhone -- is not decoded, ie might be something like 00%204 instead of 004
    }
}


public class RequestPojo {
    private String phone;
    // getter and setter
}

Here is the content of mvc-dispatcher-servlet:

<mvc:default-servlet-handler/>
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="false">
        <ref bean="customJsonHttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="com.onoff.controller">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

<bean id="multipartResolver" name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="1073741824"/>
    <property name="defaultEncoding" value="utf8"/>
</bean>

<bean id="customJsonHttpMessageConverter" class="com.onoff.util.CustomJsonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json; charset=utf-8"/>
</bean>

I've checked some suggestions before posting. URIEncoding="UTF-8" is specified in server.xml in Tomcat.

Any clues? Any help/suggestions will be highly appreciated.

Thank you in advance.

informatik01
  • 16,038
  • 10
  • 74
  • 104
user3489820
  • 1,459
  • 3
  • 22
  • 38
  • Related: [UTF-8 URL Decode / Encode](http://stackoverflow.com/questions/8147837/utf-8-url-decode-encode) – informatik01 Dec 15 '14 at 21:44
  • `00%204` isn't the same as `004`. It's the same as `00 4`. Maybe your parameters are being doubly *encoded?* – user207421 Dec 16 '14 at 02:56
  • Yes, sorry, it should be "00 4". No, my parameters aren't encoded twice – user3489820 Dec 16 '14 at 11:53
  • 1. You do not need to configure a message converter for JSON. As long as the Jackson Jars on are the class path this message converter will automatically be supported so I would remove that from the config. 2. I believe the "supportedMediaTypes" should just be "application/json" not "application/json; charset=utf-8" If you are going to set this as the media type then you have to set that in the header as the ContentType value instead of "application/json" – BrianC Mar 10 '15 at 18:06
  • 3. If it still is not working I would try changing doSomething(RequestPojo req) to doSomething(@RequestBody RequestPojo req). If that makes no difference then I would start taking a detailed look at the request because something is likely off in the way the content is coming in or the way the headers are being passed. – BrianC Mar 10 '15 at 18:06

1 Answers1

1

Please try with @ModelAttribute

public String doSomething(@ModelAttribute RequestPojo req) {
        // req.getPhone -- is not decoded, ie might be something like 00%204 instead of 004
    }
user3069716
  • 79
  • 1
  • 6