java实现跨系统接口调用简单

以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 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) JSONArray.toCollection(json, EbayUser.class);
}
} catch (Exception e) {
e.printStackTrace();
}

你可能感兴趣的:(日记)