1.在pom引用Jersey client jar
org.glassfish.jersey.core
jersey-client
2.22.1
org.glassfish.jersey.media
jersey-media-json-jackson
2.22.1
2.因springboot eureka 管理容器与客户端交互的http协议是基于Jersey 1.x 而Jersey已经到Jersey 2.x了,eureka不支持。因而采用在eureka剔除jersey2.x不支持的jar
org.springframework.cloud
spring-cloud-starter-eureka
javax.ws.rs
jsr311-api
3.创建jerseyclient工具类RestClientUtils
import java.util.Map;
import java.util.Set;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
public class RestClientUtils {
private static WebTarget target = null;// ClientBuilder.newClient().target(ApplicationConstant.REST_URL_DOCTOR_REMOTE);
public static T post(String url, String path, Object param, Map queryParam, Class rtnClass){
if (url != null) {
target = ClientBuilder.newClient().target(url);
}
WebTarget pathTarget = target.path(path);
Set keys = queryParam.keySet();
for (String string : keys) {
pathTarget = pathTarget.queryParam(string, queryParam.get(string));
}
//logger.info(pathTarget.getUri().toString());
return pathTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(param, MediaType.APPLICATION_JSON_TYPE), rtnClass);
}
public static T headerPost(String url, String path, Object param, Map queryParam, Class rtnClass,Map headerParam){
if (url != null) {
target = ClientBuilder.newClient().target(url);
}
WebTarget pathTarget = target.path(path);
Set keys = queryParam.keySet();
for (String string : keys) {
pathTarget = pathTarget.queryParam(string, queryParam.get(string));
}
Builder request = pathTarget.request(MediaType.APPLICATION_JSON_TYPE);
keys = headerParam.keySet();
for (String string : keys) {
request = request.header(string, headerParam.get(string));
}
//logger.info(pathTarget.getUri().toString());
return request.post(Entity.entity(param, MediaType.APPLICATION_JSON_TYPE), rtnClass);
}
public static T post(String url, String path, Object param, Class rtnClass){
if (url != null) {
target = ClientBuilder.newClient().target(url);
}
WebTarget pathTarget = target.path(path);
return pathTarget.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(param, MediaType.APPLICATION_JSON_TYPE), rtnClass);
}
public static String postText(String url, String path, Object param){
if (url != null) {
target = ClientBuilder.newClient().target(url);
}
WebTarget pathTarget = target.path(path);
// pathTarget.request("").post()
return pathTarget.request(MediaType.TEXT_PLAIN)
.post(Entity.entity(param, MediaType.APPLICATION_JSON_TYPE),String.class);
}
public static T get(String url, String path, Map params, Class rtnClass){
if (url != null) {
target = ClientBuilder.newClient().target(url);
}
WebTarget pathTarget = target.path(path);
Set keys = params.keySet();
for (String string : keys) {
pathTarget = pathTarget.queryParam(string, params.get(string));
}
return pathTarget.request().get(rtnClass);
}
public static T delete(String url, String path, Map params, Class rtnClass){
if (url != null) {
target = ClientBuilder.newClient().target(url);
}
WebTarget pathTarget = target.path(path);
Set keys = params.keySet();
for (String string : keys) {
pathTarget = pathTarget.queryParam(string, params.get(string));
}
return pathTarget.request().delete(rtnClass);
}
public static T put(String url, String path, Object param, Map queryParam, Class rtnClass){
if (url != null) {
target = ClientBuilder.newClient().target(url);
}
WebTarget pathTarget = target.path(path);
Set keys = queryParam.keySet();
for (String string : keys) {
pathTarget = pathTarget.queryParam(string, queryParam.get(string));
}
return pathTarget.request(MediaType.APPLICATION_JSON_TYPE)
.put(Entity.entity(param, MediaType.APPLICATION_JSON_TYPE), rtnClass);
}
public static T get(String url,String path,Class rtnClass){
if (url != null) {
target = ClientBuilder.newClient().target(url);
}
WebTarget pathTarget = target.path(path);
return pathTarget.request().get(rtnClass);
}
}