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 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
@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