第一种情况:
设置 HttpURLConnection的字符编码。connect.setRequestProperty("Accept-Charset", "UTF-8");如下代码:
@RequestMapping(value="/wszx",produces={"application/json;charset=UTF-8"})
@ResponseBody
public String getConsultationWH(HttpServletRequest request,HttpServletResponse response,Model model){
String advisoryID = request.getParameter("advisoryID");
StringBuffer json = new StringBuffer();
String jsonStr = null;
try {
if(StrUtils.isNotEmpty(advisoryID)){
URL connection;
String url = "";
url = "" ;
connection = new URL(url);
HttpURLConnection connect = (HttpURLConnection) connection.openConnection();
//设置 HttpURLConnection的请求方式
connect.setRequestMethod("POST");
//设置 HttpURLConnection的字符编码
connect.setRequestProperty("Accept-Charset", "UTF-8");
BufferedReader reader= new BufferedReader(new InputStreamReader(connect.getInputStream(),"UTF-8"));
String line = null;
while((line = reader.readLine()) != null){
json.append(line);
}
jsonStr = json.toString();
System.out.println(jsonStr);
}
} catch (Exception e) {
e.printStackTrace();
return jsonStr;
}
return jsonStr;
}
第二种情况:在spring配置文件中加入以下配置:
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>apolication/json; charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean>
以上两种基本可以解决问题。