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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq346859029/article/details/52485506

项目需求是在一个项目中,从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();
}
展开阅读全文

没有更多推荐了,返回首页