3

I have an annotation driven Spring MVC (3.2) Java web application running on Tomcat server.

I simply use Jackson JSON Mapper dependency with @ResponseBody annotation to automatically map my model objects to json response.

Here is the jackson dependency:

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.10</version>
    </dependency>

Here is my model class:

public class Person {

    String name;

    //other fields and getter and setter methods
 }

Here is the method in controller that responds to the requests:

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody Person getPersonWithId(@PathVariable String id) {

        //Query person from db and return the Person object

    }

My problem is, I have special Latin unicode characters (like Unicode Latin Capital Letter U With Diaeresis , Ü) in the fields of the Model object. The name field contains characters like ğ, ş, ç , ü and etc. I want to have unicode equivalences of these special characters as below:

ğ - \u011f

ş - \u015f

ç - \u00e7 and etc.

To be more specific I have the below json response:

{ "name": "ğasd" }

I want this to be

{ "name": "\u011Fasd" }

What is the elegant way of doing this? I could not find a way to control the encoding process in jackson mapper

Please note that I already registered spring's CharacterEncodingFilter in web.xml

<filter>  
    <filter-name>encodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
       <param-name>encoding</param-name>  
       <param-value>UTF-8</param-value>  
    </init-param>  
    <init-param>  
       <param-name>forceEncoding</param-name>  
       <param-value>true</param-value>  
    </init-param>  
</filter>  
<filter-mapping>  
    <filter-name>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping> 
simpleusr
  • 354
  • 4
  • 23
  • So to be clear, you're wanting the mapper, or something after it in the processing pipeline, to inline escape-encode certain Unicode code points? – chrylis -cautiouslyoptimistic- Jun 03 '14 at 08:00
  • Yes chrylis exactly. Actually the list of the special characters is just limited to the turkish characters and are the following: ğ - \u011f Ğ - \u011e ı - \u0131 İ - \u0130 ö - \u00f6 Ö - \u00d6 ü - \u00fc Ü - \u00dc ş - \u015f Ş - \u015e ç - \u00e7 Ç - \u00c7 – simpleusr Jun 03 '14 at 08:07
  • You'll probably need to write your own custom servlet filter to do that. It's an odd approach, though; why this strategy instead of standard UTF-8 or other encoding? – chrylis -cautiouslyoptimistic- Jun 03 '14 at 08:11
  • Actually this requirement originated from client development team. They must do alphabetic sorting in javascript code. What they say is that these special characters ğ, ş, ü and etc. makes this sorting impossible. Since I have very limited knowledge of javascript, I could not propose any workaround. May be you have a solution for this problem? That way I do not need to do anything :) – simpleusr Jun 03 '14 at 08:15
  • Have you tried to enable [JsonGenerator.Feature.ESCAPE_NON_ASCII](http://jackson.codehaus.org/1.9.0/javadoc/org/codehaus/jackson/JsonGenerator.Feature.html#ESCAPE_NON_ASCII) feature? – Alexey Gavrilov Jun 04 '14 at 21:24
  • Thanks Alexey. Actually I have convinced the client team to write their own javascript code adapted from this link: http://stackoverflow.com/questions/3630645/how-to-compare-unicode-strings-in-javascript/3633725#3633725 . But I will give a try for your solution when I am available. Thanks again... – simpleusr Jun 12 '14 at 13:07

1 Answers1

0

Try to use spring annotation RequestMapping above controller class for receveing application/json;utf-8 in all responses

@Controller
@RequestMapping(produces = {"application/json; charset=UTF-8","*/*;charset=UTF-8"})
public class MyController{
...
@RequestMapping(method = RequestMethod.GET)
    public @ResponseBody Person getPersonWithId(@PathVariable String id) {

        //Query person from db and return the Person object

    }
...
}