spring json接口开发及调用
2016-08-30 16:26
429 查看
@RequestMapping(value = "calc" , produces="text/html;charset=UTF-8") @ResponseBody public String calc( @RequestBody JSONObject jsonObj){ Long numA = jsonObj.getLong("numA") ; Long numB = jsonObj.getLong("numB") ; JSONObject json = new JSONObject() ; json.put("retCode", "001") ; json.put("res" , numA + numB) ; return json.toJSONString() ; }
public static Map<String, Object> post(String url, JSONObject obj) {
Map<String, Object> resultMap = new HashMap<String, Object>();
HttpPost post = new HttpPost(url);
post.addHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8");
StringEntity se = new StringEntity(obj.toJSONString(), "UTF-8");
se.setContentType("text/json;charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json;charset=UTF-8"));
post.setEntity(se);
HttpResponse response = null;
try {
response = new DefaultHttpClient().execute(post);
if (response != null && response.getEntity() != null) {
String out = EntityUtils.toString(response.getEntity());
resultMap = JSONObject.parseObject(out, Map.class);
} else {
resultMap.put("rtnMsg", "失败!");
}
} catch (Exception e) {
resultMap.put("rtnMsg", "失败!");
} finally {
}
return resultMap;
}
public static void main(String[] args) { JSONObject json = new JSONObject(); json.put("numA", 11); json.put("numB", 22); Map<String, Object> ret = post("http://localhost:8080/Try/calc", json); System.out.println(ret); }
相关文章推荐
- 如果是作为客户端的HTTP+JSON接口工程,没有JSP等view视图的情况下,使用Jersery框架开发绝对是第一选择。而在基于Spring3 MVC的架构下,对HTTP+JSON的返回类型也有很好
- Android开发中调用Spring CXF整合发布的WebService接口为什么抛出异常错误?
- WebWork开发JSON接口最佳实践
- xfire+spring2.5开发webservice接口的两种方式
- 自动化测试开发实际应用系列(三)HTTP JSON协议接口测试自动化实例
- Android应用开发笔记(1) 调用打电话和发短信、收短信接口、发Email
- Android应用开发笔记(1) 调用打电话和发短信、收短信接口、发Email
- vb般的VC++开发(4)--调用IDispatch接口的属性和方法
- 即时通讯开发之调用API接口快速在IOS平台的即时通讯应用开发
- rabbitmq学习11:基于rabbitmq和spring-amqp的远程接口调用
- Android应用开发笔记(1):调用打电话和发短信、收短信接口、发Email (Call, Dial, SMSManager, Broadcast, Email)
- android调用国家气象局天气预报接口json数据格式解释
- xfire+spring2.5开发webservice接口的两种方式
- Android应用开发笔记(1):调用打电话和发短信、收短信接口、发Email (Call, Dial, SMSManager, Broadcast, Email)
- 怎么根据linux c/c++ 开发的d-bus服务生成java 的调用接口文件
- vtiger CRM 二次开发接口Webservice调用实例
- 使用c++开发客户端调用XML-RPC 接口
- jQuery Ajax 调用 ASP.NET WebServices + (POST)->JSON 数据传递,一种开发高效的模式
- MongoDB与Jackson JSON结合实现接口敏捷开发
- cxf+spring发布webservice接口及接口的调用