一个项目调用另一个项目接口,传递json参数

项目需求是在一个项目中,从mysql获取数据,转换成json串,用HttpClient调用另一个项目的接口,并传递json数据;另一个项目接收json​数据,解析后再写入到oracle中。

获取mysql数据,这里不写详细获取方法和数据库内容了。只是传输和接收的2个方法:

/**
 * 
 * 方法描述:调用项目接口,传递json串参数
 * 
 * @param
 * @author:tinker
 * @throws ParseException
 * @data:日期:2016年9月9日10:50:52
 */
@RequestMapping("/postJson")
public String postJson(ConferenceInfo conferenceInfo, Model model, HttpServletRequest request, HttpServletResponse response) throws ParseException {
    // 从数据库获取信息
    List<ConferenceInfo> conferenceInfoList = new ArrayList<>();
    conferenceInfoList = conferenceNoticeTypeService
            .conferceInfoFind(conferenceInfo);
    // 转换成json
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.registerJsonValueProcessor(Date.class,
            new JsonDateValueProcessor());
    JSONObject json = new JSONObject();
    json = JSONObject.fromObject(conferenceInfoList, jsonConfig);// stringBuffer
    System.out.println("传入参数:" + json);
    HttpFa2 httpFa2 = new HttpFa2();
    // 发送 POST 请求 调用接口 参数1 接口名(项目后 不带/) 参数二 json对象 参数三 request
    JSONObject sr = httpFa2.doPost("conferenceInfo/getJson", json, request);
    System.out.println("返回参数:" + sr);
    return null;
}

HttpFa2.java 代码:

package com.icp.utils;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpFa2 {

/**
 * post请求
 * 
 * @param url
 * @param json
 * @return
 */

public static JSONObject doPost(String method, JSONObject date,HttpServletRequest request) {
    HttpClient client = HttpClients.createDefault();
    // 将接口地址和接口方法拼接起来
    String url = "http://10.224.11.13:8080/icp1.0/" + method;
    HttpPost post = new HttpPost(url);
    JSONObject response = null;
    try {
        StringEntity s = new tringEntity(date.toString());
        s.setContentEncoding("UTF-8");
        s.setContentType("application/json");
        post.setEntity(s);
        post.addHeader("content-type", "text/xml");
        // 调用Fa接口
        HttpResponse res = client.execute(post);
        String response1 = EntityUtils.toString(res.getEntity());
        System.out.println("************");
        System.out.println(response1);
        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = res.getEntity();
            String result = EntityUtils.toString(res.getEntity());// 返回json格式:
            response = JSONObject.fromObject(result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }
}

另一个项目接收json代码:

/**
 * 接收传递的json参数解析
 * 
 * @param request
 * @param response
 * @return
 * @data:日期:2016年9月9日10:50:52
 */
@RequestMapping("/getJson")
public String getJson(HttpServletRequest request,
        HttpServletResponse response) {
    StringBuffer json = new StringBuffer();
    String line = null;
    try {
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null) {
            System.out.println("line:" + line);
            json.append(line);
        }
    } catch (Exception e) {
        System.out.println(e.toString());

    }
    System.out.println(json.toString());
    return json.toString();
}
ps:主要解决在webservice中,我们想从另外一个项目调用webservice项目接口,也就是跨项目调用接口 这里主要用到了xfire wsdl 废话不说了 直接上东西 1. 首先新建一个项目 2. 在src下创建两个文件: a) 第一个是你想要访问的webservice的接口,比如我想访问的接口是 ReleaseService 那就在当前项目创建一个ReleaseService接口(接口中的方法必须和你想要访问的webservice的接口中的方法相同) b) 第二个是你的调用类 3. 导入相应的jar包,这些包不能引用,一定要复制到lib文件夹下面在引用 4. 具体的实现代码 TestWebService方法的代码: package com.isanta.webServiceTest; import java.io.InputStream; import java.net.MalformedURLException; import java.util.Properties; import java.util.Scanner; import org.codehaus.xfire.XFireFactory; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; public class TestWebService { /** * @param args */ public static void testWebService() throws MalformedURLException, Exception{ // TODO Auto-generated method stub /** *这里是我的参数放在了properties文件中,我在读取里面的参数,这里我们也可以通过方法传参数 *如 : testWebService(String url,String xMlStr)() 那么在调用的时候就可以直接传进来了 *url 是你访问的webservice 的tomcat 的服务器地址 */ Properties pro = new Properties(); InputStream in = null; in = TestWebService.class.getResourceAsStream("/request.properties"); pro.load(in); String url = pro.getProperty("url"); String xMLstr = pro.getProperty("xMLstr"); Service s=new ObjectServiceFactory().create(ReleaseService.class); XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire()); System.out.println("url="+url); try { //这里就是获取webservice的接口的实例对象 ReleaseService seleaseService=(ReleaseService) xf.create(s,url); System.out.println("进入接口----------------->请求报文:"+xMLstr); //这里就是调用你需要的接口的方法 String st=seleaseService.queryReceiptDatas(xMLstr); System.out.print(st); } catch(Exception e) { e.printStackTrace(); } } } 5. 将整个项目打包成jar 6. 将打好的jar包引入到你想要调用项目中,然后就想 正常的代码一样来调用,如: import java.net.MalformedURLException; import com.isanta.webServiceTest.TestWebService; public class Test { public static void main(String[] args) throws MalformedURLException, Exception { TestWebService.testWebService(); } }
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值