您的位置:首页 > 编程语言 > Java开发

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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: