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);
}
}