使用exchange
import com.alibaba.fastjson.JSON;
import com.crd.service.routing.bean.AddrInfo;
import com.crd.service.routing.bean.Response;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.Charset;
public class RestClientUtil {
public static <T> T exchange(String url, HttpMethod method, Class<T> response, String request) {
HttpHeaders headers = new HttpHeaders();
MimeType mimeType = MimeTypeUtils.parseMimeType("application/json");
MediaType mediaType = new MediaType(mimeType.getType(), mimeType.getSubtype(), Charset.forName("UTF-8"));
headers.setContentType(mediaType);
HttpEntity<String> entity = new HttpEntity<>(request, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<T> resultEntity = restTemplate.exchange(url, method, entity, response);
return resultEntity.getBody();
}
public static void main(String[] args) {
String url = "http://127.0.0.1:8088/route/addr/安徽";
AddrInfo addrInfo = new AddrInfo("安徽", "127.0.0.1:9877");
Response response = RestClientUtil.exchange(url, HttpMethod.DELETE, Response.class,
null);
System.out.println(response);
}
}