RestTemplat发送http请求通用工具类

1.spring配置resttemplate


2.通用工具类

package com.yh.finance.purchase.utils;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import com.yh.finance.purchase.exception.PurchaseException;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.StringUtils;

import org.springframework.http.*;

import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

public class RestTemplateUtils {

private static MediaTypemediaType = MediaType.APPLICATION_JSON_UTF8;

    private static int successCode =200;

    private static StringmsgCodeLOMS ="msgCode";

    private static StringsuccessCodeLOMS ="1";

    private static StringdataLOMS ="data";

    private static StringmessageLOMS ="message";

    public static ListsendHttpRequest(RestTemplate restTemplate, String url, P param, Class object) {

return sendHttpRequest(restTemplate,url,param,null,object);

    }

public static ListsendHttpRequest(RestTemplate restTemplate, String url, List

param, Class object) {

return sendHttpRequest(restTemplate,url,null,param,object);

    }

/**

    * 发送http json格式参数请求

    * @param restTemplate

    * @param url http地址

    * @param param1 参数

    * @param param2 list参数

    * @param object 返回类型

    * @param   返回类型

    * @param

参数类型

    * @return

    */

    public static ListsendHttpRequest(RestTemplate restTemplate, String url, P param1, List

param2, Class object) {

HttpHeaders headers =new HttpHeaders();

        //定义请求参数类型,这里用json所以是MediaType.APPLICATION_JSON

        headers.setContentType(mediaType);

        //RestTemplate带参传的时候要用HttpEntity对象传递

        ResponseEntity entity =null;

        if(param1 !=null && param2 ==null) {

HttpEntity

queryRequest =new HttpEntity<>(param1, headers);

            //---------参数转json---------------

            System.out.println("--------------参数转json--------------------");

            System.out.println(JSON.toJSONString(param1));

            System.out.println("--------------------------------------------");

            //----------------------------------------------

            entity = restTemplate.postForEntity(url, queryRequest, String.class);

        }else if(param2 !=null && param1 ==null) {

HttpEntity> queryRequest =new HttpEntity<>(param2, headers);

            //---------参数转json---------------

            System.out.println("--------------参数转json--------------------");

            System.out.println(JSON.toJSONString(param2));

            System.out.println("--------------------------------------------");

            //----------------------------------------------

            entity = restTemplate.postForEntity(url, queryRequest, String.class);

        }else if (param1 ==null && param2 ==null){

HttpEntity

queryRequest =new HttpEntity<>(null, headers);

            entity = restTemplate.postForEntity(url, queryRequest, String.class);

        }else{

throw new PurchaseException("请输入正确的参数");

        }

HttpStatus httpStatusCode = entity.getStatusCode();

        int statusCode = httpStatusCode.value();

        List list =new ArrayList<>();

        //如果返回成功

        if(statusCode ==successCode) {

String body = entity.getBody();

            Map jsonMap = JSONObject.parseObject(body,Map.class);

            if(jsonMap !=null){

String msgCode = (String) jsonMap.get(msgCodeLOMS);

                String message = (String) jsonMap.get(messageLOMS);

                if(StringUtils.isNotBlank(msgCode) && msgCode.equals(successCodeLOMS)) {

JSONArray jsonArray =null;

                    if(jsonMap.get(dataLOMS) !=null) {

jsonArray = JSONArray.fromObject(jsonMap.get(dataLOMS));

                    }

if(jsonArray !=null && jsonArray.size() >0) {

list = JSONArray.toList(jsonArray, object);

                    }

}else{

throw new PurchaseException(message);

                }

}else{

throw new PurchaseException("LOMS接口调用异常");

            }

}else{

throw new PurchaseException("LOMS接口调用异常");

        }

return list;

    }

}


3,调用工具类

//集单提交接口地址

String url = Config.getString("submitCollectLOMS");

RestTemplateUtils.sendHttpRequest(restTemplate,url,collectPurchaseReqLOMSList,CollectPurchaseLOMS.class);

你可能感兴趣的:(RestTemplat发送http请求通用工具类)