javax.ws.rs-api
https://blog.csdn.net/qq_22177809/article/details/86592660
jersey
服务端简单的使用:https://blog.csdn.net/github_38395241/article/details/70265379
客户端简单使用:https://blog.csdn.net/Maxiao1204/article/details/118720979
在 spring-boot-starter-web 中用 jetty 代替 tomcat
在 spring-boot-starter-web 移除现有的依赖项,并把下面这些添加进去。
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
客户端使用
配置请求响应类
package com.example.demo.jersey;
public class ClientResponse<T> {
private int status;
private T entity;
private String message;
private ClientResponse() {
}
private ClientResponse(int status, T entity) {
this.status = status;
this.entity = entity;
}
private ClientResponse(String message, int status) {
this.status = status;
this.message = message;
}
public int getStatus() {
return this.status;
}
public void setStatus(int status) {
this.status = status;
}
public T getEntity() {
return this.entity;
}
public void setEntity(T entity) {
this.entity = entity;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public static <T> ClientResponse successResponse(T entity) {
return new ClientResponse( 200,entity);
}
public static <T> ClientResponse errorResponse(String message) {
return new ClientResponse(message, 400);
}
}
配置HttpClientUtil工具类
package com.example.demo.jersey;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class HttpClientUtil {
private Client client;
private String baseUrl;
private WebTarget webTarget;
public enum HttpMethod {
//get
GET("GET"),POST("POST"),PUT("PUT"),DELETE("DELETE");
String method;
HttpMethod(String method){
this.method = method;
}
}
private HttpClientUtil(String baseUrl){
this.client = ClientBuilder.newClient();
this.baseUrl = baseUrl;
this.webTarget = this.client.target(this.baseUrl);
}
public static <T> ClientResponse httpRequest(String url , HttpMethod httpMethod, Object params, Class<T> classType){
HttpClientUtil httpClientUtil = null;
Response response = null;
try {
httpClientUtil = new HttpClientUtil(url);
switch (httpMethod) {
case GET:
response = httpClientUtil.webTarget.request().get();
break;
case POST:
response = httpClientUtil.webTarget.request().post(Entity.entity(params, MediaType.APPLICATION_JSON_TYPE));
break;
case PUT:
response = httpClientUtil.webTarget.request().put(Entity.entity(params, MediaType.APPLICATION_JSON_TYPE));
break;
case DELETE:
response = httpClientUtil.webTarget.request().delete();
break;
default:
response = httpClientUtil.webTarget.request().get();
}
if (response.getStatus() != 200) {
return ClientResponse.errorResponse("Failed with HTTP error code : " + response.getStatus());
} else {
return ClientResponse.successResponse(response.readEntity(classType));
}
} finally {
if (response != null){
response.close();
}
if (httpClientUtil != null){
httpClientUtil.close();
}
}
}
public void close() {
this.client.close();
}
}
测试
import java.util.*;
public class Demo {
public static void main(String[] args) {
String url = "http://127.0.0.1:8056/heren/api/bill/pat-master-index/patient-id?patientId=11105298";
ClientResponse response = HttpClientUtil.httpRequest(url, HttpClientUtil.HttpMethod.GET,null, Map.class);
if (response.getStatus() == 200){
Map map = (Map) response.getEntity();
System.out.println(map.get("name"));
} else {
System.out.println(response.getMessage());
}
}
}