直接上代码

/**
* 测试生成json数据
*/
@Test
public void synYxGoodsInfoTest() {
try {
String url = "http://10.118.44.14:8070/teshi-web/goods/synYxGoods";
GoodsInfo goodsInfo = new GoodsInfo();
goodsInfo.setGoods_id(111);
goodsInfo.setGoodsName("1231213");
goodsInfo.setBrand(1);
goodsInfo.setType(1);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
String jsonstr = JSON.toJSONString(goodsInfo);
StringEntity se = new StringEntity(jsonstr);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost); //输出调用结果
if (response != null && response.getStatusLine().getStatusCode() == 200) {
String result = EntityUtils.toString(response.getEntity());
// 生成 JSON 对象
JSONObject obj = JSONObject.parseObject(result);
String errorcode = obj.getString("errorcode");
if ("000".equals(errorcode)) {
System.out.println("addHkfishOrder_request_success");
}
}
} catch (Exception e) {
System.out.println("======回不来了=======");
}
}

解析

/**
* 接收数据解析,并通过流返回数据
*/
@RequestMapping(value = "/synYxGoods")
@ResponseBody
public String synYxGoods(HttpServletResponse response, HttpServletRequest request) throws IOException {
//String json = request.getParameter("param"); //这是通过通过get方式去url 拼接的键值对,post方式取不到值。
request.setCharacterEncoding("UTF-8"); //返回页面防止出现中文乱码
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));//post方式传递读取字符流
String jsonStr = null;
StringBuilder result = new StringBuilder();
try {
while ((jsonStr = reader.readLine()) != null) {
result.append(jsonStr);
}
} catch (IOException e) {
e.printStackTrace();
}
reader.close();// 关闭输入流
JSONObject jsonObject = JSONObject.parseObject(result.toString()); // 取一个json转换为对象
logger.info(jsonObject);
GoodsInfo goodsInfo = new GoodsInfo();
Date date = new Date();
goodsInfo.setAccess_code1("001");
goodsInfo.setAccess_code1("001");
goodsInfo.setGoodsName(jsonObject.getString("goodsName")); //通过键取到值,再将值封装到类里面。
goodsInfo.setType(Integer.parseInt(jsonObject.getString("type")));
List<ResultData<String>> data = yxGoodsService.synYxGoodsInfo(goodsInfo);
String json_str = JSON.toJSONString(data);
return write(response, json_str);
}

文章转载至:https://www.cnblogs.com/sharpest/p/6406013.html

最新文章

  1. 深入学习jQuery动画控制
  2. 关于DOM
  3. CSS对浏览器的兼容性(IE和Firefox)技巧整理
  4. 用Js的eval解析JSON中的注意点
  5. &lt;四&gt;面向对象分析之UML核心元素之用例
  6. 【转】STL空间配置器
  7. [React] React Fundamentals: State Basics
  8. impdp/expdp 总结
  9. AppCode3 常用 设置 及 快捷键 (持续更新)
  10. 高性能MySQL第2,3章性能相关 回顾笔记
  11. gzip 与 gunzip 语法与示例
  12. spring boot / cloud (十二) 异常统一处理进阶
  13. 配置MQTT服务器
  14. VB6 变量定义作用域的一个奇特形式
  15. 帝国cms栏目别名怎样调用?栏目名称太短了
  16. 02: shell中的if、case、for等语句
  17. 【BZOJ4149】[AMPPZ2014]Global Warming 单调栈+RMQ+二分
  18. NOI2019省选模拟赛 第六场
  19. 【luogu P4180 严格次小生成树[BJWC2010]】 模板
  20. /usr/bin/python与/usr/bin/env python的区别

热门文章

  1. KEIL expected an identifier
  2. IT菜鸟之思科模拟实验(PT)
  3. AD中如何解决 同一局域网下证书冲突问题
  4. 为Go项目编写Makefile
  5. MySQL必知必会复习笔记(1)
  6. UiPath中恢复依赖项失败的解决方法
  7. selenium常用方法集合
  8. 四、配置及使用Zabbix监控系统
  9. Idea的安装破解及配置
  10. 微软发布了Visual Studio 2022 Preview 1 以及.NET 6 Preview 5