Spring MVC3 return JSON data in Chinese garbled problem solving

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:

  1. 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 processer     import 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  */    @Controller    public 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);         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (JSON);                 //Solution Two                servletoutputstream os = response.getoutputstream ();  //get output stream               os.write (Json.getBytes ( Charset.forname ("GBK"));  //writes JSON data to the stream                os.flush ();                 //Plan One               Response.setcharacterencoding ("UTF-8");  //set encoding format                response.setcontenttype ("text/html");    //setting data Format                PrintWriter out =  Response.getwriter ();  //Get Write Object                out.print (JSON);  //writes JSON data to the stream                out.flush ();          }           catch (exception e)            {               E.printstacktrace ();           }            //return  " Home ";       }  }

there's another way : Set   @RequestMapping    

Idea: Use @ResponseBody annotations to return the JSON string directly, in order to prevent Chinese garbled, set the @RequestMapping produces parameter to "text/ Html;charset=utf-8 " can.

[Java] view plaincopy

  1. @RequestMapping (value= "/home/writejson", method=requestmethod.get, produces =  "text/html; Charset=utf-8 ")    @ResponseBody   public 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 solving

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.