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.

5
  • Related: UTF-8 URL Decode / Encode Commented Dec 15, 2014 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? Commented Dec 16, 2014 at 2:56
  • Yes, sorry, it should be "00 4". No, my parameters aren't encoded twice Commented Dec 16, 2014 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" Commented Mar 10, 2015 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. Commented Mar 10, 2015 at 18:06

1 Answer 1

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 } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.