zoukankan      html  css  js  c++  java
  • SpringBoot RestTemplate使用泛型

    ps:推荐使用Feign

    package com.example.demo;
    
    import com.fasterxml.jackson.databind.JavaType;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.type.TypeFactory;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.core.ParameterizedTypeReference;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.client.RestTemplate;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * SpringBoot restTemplate使用泛型,Result、Person、服务端代码就不放出了
     */
    @SpringBootTest
    class BusinessApplicationTests {
    
        @Autowired
        public RestTemplate restTemplate;
    
        private static final ObjectMapper objectMapper = new ObjectMapper();
    
        @Test
        void test1() {
            // Result<List>
            String url = "http://127.0.0.1/1";
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(Result.class, List.class);
            ParameterizedTypeReference<Object> reference = ParameterizedTypeReference.forType(javaType);
            ResponseEntity<Object> entity = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(null), reference);
        }
    
        @Test
        void test2() {
            // Result<List<Person>>
            String url = "http://127.0.0.1/2";
            TypeFactory typeFactory = objectMapper.getTypeFactory();
            JavaType inner = typeFactory.constructParametricType(List.class, Person.class);
            JavaType javaType = typeFactory.constructParametricType(Result.class, inner);
            ParameterizedTypeReference<Object> reference = ParameterizedTypeReference.forType(javaType);
            ResponseEntity<Object> entity = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(null), reference);
        }
    
        @Test
        void test3() {
            // Result<Person>
            String url = "http://127.0.0.1/3";
            TypeFactory typeFactory = objectMapper.getTypeFactory();
            JavaType javaType = typeFactory.constructParametricType(Result.class, Person.class);
            ParameterizedTypeReference<Object> reference = ParameterizedTypeReference.forType(javaType);
            ResponseEntity<Object> entity = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(null), reference);
        }
    
        /**
         * 一个通用方法
         *
         * @param url
         * @param param
         * @param parameterClasses
         * @return
         */
        public ResponseEntity<Object> _doPost(String url, Map<String, String> param, Class<?>... parameterClasses) {
            TypeFactory typeFactory = objectMapper.getTypeFactory();
            ParameterizedTypeReference<Object> reference = null;
            JavaType javaType = null;
            int length = parameterClasses.length;
            if (length == 1) {
                javaType = typeFactory.constructParametricType(Result.class, parameterClasses);
            } else {
                javaType = typeFactory.constructParametricType(parameterClasses[length - 2], parameterClasses[length - 1]);
                for (int i = length - 3; i >= 0; i--) {
                    javaType = typeFactory.constructParametricType(parameterClasses[i], javaType);
                }
                javaType = typeFactory.constructParametricType(Result.class, javaType);
            }
            reference = ParameterizedTypeReference.forType(javaType);
            return restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(param), reference);
        }
    
        @Test
        void test4() {
            String url1 = "http://127.0.0.1/1";
            String url2 = "http://127.0.0.1/2";
            String url3 = "http://127.0.0.1/3";
            ResponseEntity<Object> entity1 = _doPost(url1, null, List.class);
            ResponseEntity<Object> entity2 = _doPost(url2, null, List.class, Person.class);
            ResponseEntity<Object> entity3 = _doPost(url3, null, Person.class);
    
        }
    }
    
  • 相关阅读:
    基于WebDriverAgent代理服务,实现iOS手机app自动化测试的框架搭建
    ruby 中%Q %q %W %w %x %r %s的用法
    ruby中 Win32ole的各应用操作方法(word excel Outlook IE)
    dubbo服务自动化测试搭建
    Spring AOP 结合自定义注解的使用
    Mysql 中 JSON_CONTAINS、JSON_ARRAY 的使用
    Spring Cloud Eureka 的一些配置
    Spring Cloud 整合 Spring Boot Admin
    Nginx 泛域名解析配置
    Spring Data Solr
  • 原文地址:https://www.cnblogs.com/n031/p/13664201.html
Copyright © 2011-2022 走看看