在解决这一问题的时候,主要需要解决两个问题,
1 在struts2中接收并返回json数据
2 调用第三方接口
解决,
1 在struts2中应该是提供了常规的参数封装解决方案,但是一直实现不了,后来找到这篇博客,并且使用原生api的方式实现了。
https://www.cnblogs.com/zhongsir/p/6847045.html
包括如何返回json数据,参考了这篇博客,使用原生api实现了
https://blog.csdn.net/feinifi/article/details/81114268
2 使用的是apache commons中提供的一个工具 Apache HttpComponents
代码如下:
public static String doPostRsp(String url, String param) {
String responseContent = "";//返回值
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
httpPost.addHeader("charset", "utf-8");
httpPost.addHeader("cache-control", "no-cache");
httpPost.setEntity(new StringEntity(param));
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
responseContent = EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
httpClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return responseContent;
}