java实现跨系统接口调用简单
2017-11-14 11:26
711 查看
以spring mvc框架为例 抛开权限,认证限制 controller代码
package com.it.portal.controller.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.it.portal.controller.BaseController;
import com.it.portal.service.SystemService;
/**
* @author zhangqg
* @date 2017年11月8日
* 类说明
*/
@Controller
@RequestMapping("/api")
public class EbayUserApiController extends BaseController {
@Autowired
private SystemService systemService;
@RequestMapping(value = "/ebayUser/getAllEbayUser", method = RequestMethod.POST)
@ResponseBody
public Object getEbayUserList()
{
return systemService.getEbayUserList();
}
}
接口调用代码
CommonsProperty commonsProperty=(CommonsProperty)SpringUtils.getBean("commonsProperty");
HttpPost httpRequst = new HttpPost(commonsProperty.getUriAPI());// 创建HttpPost对象
httpRequst.addHeader("Content-type","application/json; charset=utf-8");
httpRequst.addHeader("Accept", "application/json");
List<EbayUser> users = null;
try {
HttpResponse httpResponse = HttpClients.createDefault().execute(httpRequst);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
String result = EntityUtils.toString(httpEntity);// 取出应答字符串
JSONArray json = JSONArray.fromObject(result);// userStr是json字符串
users = (List<EbayUser>) JSONArray.toCollection(json, EbayUser.class);
}
} catch (Exception e) {
e.printStackTrace();
}
package com.it.portal.controller.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.it.portal.controller.BaseController;
import com.it.portal.service.SystemService;
/**
* @author zhangqg
* @date 2017年11月8日
* 类说明
*/
@Controller
@RequestMapping("/api")
public class EbayUserApiController extends BaseController {
@Autowired
private SystemService systemService;
@RequestMapping(value = "/ebayUser/getAllEbayUser", method = RequestMethod.POST)
@ResponseBody
public Object getEbayUserList()
{
return systemService.getEbayUserList();
}
}
接口调用代码
CommonsProperty commonsProperty=(CommonsProperty)SpringUtils.getBean("commonsProperty");
HttpPost httpRequst = new HttpPost(commonsProperty.getUriAPI());// 创建HttpPost对象
httpRequst.addHeader("Content-type","application/json; charset=utf-8");
httpRequst.addHeader("Accept", "application/json");
List<EbayUser> users = null;
try {
HttpResponse httpResponse = HttpClients.createDefault().execute(httpRequst);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
String result = EntityUtils.toString(httpEntity);// 取出应答字符串
JSONArray json = JSONArray.fromObject(result);// userStr是json字符串
users = (List<EbayUser>) JSONArray.toCollection(json, EbayUser.class);
}
} catch (Exception e) {
e.printStackTrace();
}
相关文章推荐
- java调用WEBSERVICE接口简单实现
- 使用Java8的函数式接口@FunctionalInterface实现简单异步调用
- 使用Java8的函数式接口@FunctionalInterface实现简单异步调用
- java 调用 webservice 接口的简单实现
- java运用jni调用dll(含源码)实现屏蔽系统热键和任务栏
- java中读写锁的实现及使用读写锁简单实现缓存系统的实例
- 用RMI实现一个简单的实时聊天系统(java语言)
- java实现的简单文件管理系统
- JAVA调用接口实现
- java简单实现webservice接口
- java中读写锁的实现及使用读写锁简单实现缓存系统的实例
- 调用新浪微博接口实现新浪微博简单搜索
- Java1.6.0实现调用操作平台桌面系统
- java接口类型变量能调用已经被实现的这个接口的方法吗
- JNI简单实现Java调用C++/C的HelloWorld
- ICTCLAS分词系统Java调用接口在Eclipse中的安装- Java编程
- 关于Java中Scanner对象的hasNext()方法对实现Readable接口的对象中的read()方法调用的探讨
- 调用系统命令实现删除文件的操作,java调用系统命令实现删除文件的操作
- Java实现的简单的WebService服务发布和Client调用例子
- 跟老紫竹学Java-图书销售系统-服务接口和实现