欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java调用http接口通过RestTemplate调用

程序员文章站 2022-06-26 12:05:42
...
package com.ruoyi.common.utils.webservice;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Field;
import java.util.List;

/***
 @ClassName RestTemplate
 @Author Mr.lyb
 @Date 2020-7-8 16:54  星期三
 ***/
public class RestTemplateUtils {
/***
 * @MethodName: buildParamMaps
 * @Description: 读取要传入的数据
 * @Param: [params]
 * @Return: org.springframework.util.MultiValueMap<java.lang.String,java.lang.String>
 * @Date: 2020-7-8 16:55
   @Author: Mr.lyb   help QQ [email protected]
**/
    public static MultiValueMap<String, String> buildParamMaps(SecurityPlatformParams params){
        Field[] fields = params.getClass().getDeclaredFields();
        if (ArrayUtils.isEmpty(fields)){
            return null;
        }
        MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
        try {
            for (Field field : fields){
                field.setAccessible(true);
                paramMap.add(field.getName(), StringUtils.defaultIfNull(field.get(params), ""));
            }
        }catch (IllegalAccessException e){
            e.printStackTrace();
            return null;
        }
        return paramMap;
    }
/**
 * @MethodName: post
 * @Description: 请求接口返回数据
 * @Param: [restTemplate, url, paramMap]
 * @Return: java.lang.String
 * @Date: 2020-7-8 16:57
   @Author: Mr.lyb
**/
    public static String post(RestTemplate restTemplate, String url, MultiValueMap paramMap) throws Exception{
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(paramMap, httpHeaders);
        String resultStr;
        try {
            resultStr = restTemplate.postForObject(url, httpEntity, String.class);
        } catch (RestClientException e) {
            throw e;
        }
        return resultStr;
    }


    public static List<T> Entity(RestTemplate restTemplate, String url, MultiValueMap paramMap, Class<?> clazz) throws Exception{
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(paramMap, httpHeaders);
        String resultStr;
        try {
            resultStr = restTemplate.postForObject(url, httpEntity, String.class);
        } catch (RestClientException e) {
            throw e;
        }

       return (List)JSON.parseArray(resultStr, clazz);

    }

}
//test
 public static void main(String[] args) throws Exception {
        String url="http://localhost:8085/examxx_war/sysDept/yyb";//访问的接口
       RestTemplate restTemplate = new RestTemplate();//模板
       MultiValueMap multiValueMap = null;//定义MultiValueMap对象
       SecurityPlatformParams sp = new SecurityPlatformParams();//这个是对象,里面存放实体类属性用来引用
       sp.setParentId("101");//放入要传入的值,
        MultiValueMap<String, String> stringStringMultiValueMap = RestTemplateUtils.buildParamMaps(sp);//解析
        String post = RestTemplateUtils.post(restTemplate, url, stringStringMultiValueMap);//调用post

        List<SysDept> entity = (List)RestTemplateUtils.Entity(restTemplate, url, stringStringMultiValueMap, SysDept.class);//强转成list对象

       for (SysDept s :entity){
           System.out.println(s.getDeptName());
       }
        System.out.println(entity);

    }

1,返回 json格式

2,返回List集合对象

相关标签: java