I checked some information online and felt it was quite complicated. Here, I used two very simple methods to solve the problem of Chinese garbled code.
Spring version: 3.2.2.RELEASE
Jackson JSON version: 2.1.3
Solution: Controller's method directly writes String type json data to the network stream through response.
Use Jackson's ObjectMapper to convert Java objects to JSON data of type String.
In order to avoid Chinese garbled code, you need to set character encoding formats, such as: 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","Li Si"); map.put("3","Wang Wu"); map.put("4", "Jackson"); String json = ""; try { json = mapper.writeValueAsString(map); System.out.println(json); //Scheme 2 ServletOutputStream os = response.getOutputStream(); //Get the output stream os.write(json.getBytes(Charset.forName("GBK"))); //Write json data into the stream os.flush(); //Scheme 1 response.setCharacterEncoding("UTF-8"); //Set the encoding format response.setContentType("text/html"); //Set the data format PrintWriter out = response.getWriter(); //Get the write object out.print(json); //Write json data into the stream out.flush(); } catch(Exception e) { e.printStackTrace(); } //Return "home"; }}There is another method: set the produces parameter of @RequestMapping, the code is as follows:
Idea: Use the @ResponseBody annotation to directly return the json string. In order to prevent Chinese garbled code, set the producers parameter of @RequestMapping 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","Li Si"); map.put("3","Wang Wu"); map.put("4", "Jackson"); String json = ""; try { json = mapper.writeValueAsString(map); System.out.println(json); } catch(Exception e) { e.printStackTrace(); } return json;}The operation results are shown in the figure below:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.