Quick Start:http://hc.apache.org/httpcomponents-client-ga/index.htmljava
=================================web
SpringMVC 使用@ResponseBody返回json 中文乱码spring
方法一,使用(produces = "application/json; charset=utf-8"):apache
@RequestMapping(value = "/for", method = {RequestMethod.GET}, produces = "application/json; charset=utf-8") @ResponseBody public Object forr() { User user1 = new User(); user1.setId(2); user1.setName("嘿嘿"); user1.setPass("456"); User user = new User(1,"张三","123",user1); String jsonString = JSON.toJSONString(user); return jsonString; }
方法二,在spring-mvc.xml中添加:json
<!-- 处理请求返回json字符串的中文乱码问题 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
//get请求 private static void doGet() throws IOException, ClientProtocolException { //建立一个 httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); //建立一个Get请求 // http://localhost:8080/Shiro/for HttpGet httpGet = new HttpGet("http://localhsot:8080/Shiro/for"); //HttpGet httpGet = new HttpGet("http://www.baidu.com?id =123&name=hehe"); //执行请求 CloseableHttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); //获得状态码 if (response.getStatusLine().getStatusCode() == 200) { System.out.println("---"); } //将HTTP响应体 转换成String对象 System.out.println( EntityUtils.toString(entity,"utf-8")); }
//get请求携带数据 private static void doGetWithParm() throws IOException, ClientProtocolException, URISyntaxException { //建立一个 httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); //建立一个Get请求 URIBuilder uri = new URIBuilder("http://www.sogou.com/web"); //添加查询参数 uri.addParameter("query","花千骨"); HttpGet httpGet = new HttpGet(uri.build()); //执行请求 CloseableHttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); //获得状态码 if (response.getStatusLine().getStatusCode() == 200) { System.out.println("---"); } //将HTTP响应体 转换成String对象 System.out.println( EntityUtils.toString(entity,"utf-8")); }
// post请求 public void doPost() throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); // 建立一个Post对象 HttpPost httpPost = new HttpPost("http://targethost/login"); // 执行post请求 CloseableHttpResponse execute = httpclient.execute(httpPost); EntityUtils.toString(execute.getEntity(), "utf-8"); }
// post请求携带参数 public void doPostWithParm() throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); // 建立一个Post对象 HttpPost httpPost = new HttpPost("http://targethost/login"); //建立参数 List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("username", "vip")); nvps.add(new BasicNameValuePair("password", "secret")); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); // 执行post请求 CloseableHttpResponse execute = httpclient.execute(httpPost); EntityUtils.toString(execute.getEntity(), "utf-8"); }