Spring MVC3 return JSON data in Chinese garbled problem resolution (GO)

Source: Internet
Author: User

Spring MVC3 return JSON data in Chinese garbled problem solving

Check out some information on the internet, feeling more complex, here, I use two very simple methods to solve the problem of Chinese garbled.

Spring Version: 3.2.2.RELEASE

Jackson JSON version: 2.1.3

WORKAROUND: The controller's method directly writes the string type of JSON data to the network stream through response.

Use Jackson's objectmapper to convert a Java object to a string type of JSON data.

In order to avoid Chinese garbled characters, we need to set character encoding format, for example: UTF-8, GBK, etc.

The code is as follows:

Import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.Controller; Import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.requestparam;import Com.fasterxml.jackson.databind.ObjectMapper; Jsckson JSON processerimport java.util.*;import javax.servlet.servletoutputstream;import javax.servlet.http.*; Import Java.io.printwriter;import java.nio.charset.charset;/** * Created with IntelliJ idea 12.0 * date:2013-03-15 * Time    : 16:17 */@Controllerpublic class HomeController {@RequestMapping (value= "/home/writejson", Method=requestmethod.get)        public void Writejson (HttpServletResponse response) {Objectmapper mapper = new Objectmapper ();        hashmap<string,string> map = new hashmap<string,string> ();        Map.put ("1", "Zhang San");        Map.put ("2", "John Doe"); Map.put ("3", "Harry");        Map.put ("4", "Jackson");        String json = "";            try {json = mapper.writevalueasstring (map);            SYSTEM.OUT.PRINTLN (JSON); Scenario two Servletoutputstream OS = Response.getoutputstream (); Gets the output stream Os.write (Json.getbytes (Charset.forname ("GBK"));            Writes the JSON data to the stream Os.flush (); Programme one response.setcharacterencoding ("UTF-8");   Set the encoding format response.setcontenttype ("text/html"); Set data format PrintWriter out = Response.getwriter (); Gets the Write Object Out.print (JSON);        Writes the JSON data to the stream Out.flush ();        } catch (Exception e) {e.printstacktrace ();    }//return "Home"; }}

  

Another way : Set the produces parameter for the @RequestMapping, as shown in the code:

Idea: Use @ResponseBody annotations to return the JSON string directly, in order to prevent Chinese garbled, the @requestmapping produces parameter is set to "Text/html;charset=utf-8".

@RequestMapping (value= "/home/writejson", method=requestmethod.get, produces = "text/html;charset=utf-8") @ Responsebodypublic Object Writejson (httpservletresponse response) {        Objectmapper mapper = new Objectmapper ();        hashmap<string,string> map = new hashmap<string,string> ();        Map.put ("1", "Zhang San");        Map.put ("2", "John Doe");        Map.put ("3", "Harry");        Map.put ("4", "Jackson");        String json = "";        Try        {            json = mapper.writevalueasstring (map);            SYSTEM.OUT.PRINTLN (JSON);        }        catch (Exception e)        {            e.printstacktrace ();        }        return JSON;}

  

The results of the operation are as follows:

Spring MVC3 return JSON data in Chinese garbled problem resolution (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.