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 移除现有的依赖项,并把下面这些添加进去。

  1. <groupId>org.springframework.boot</groupId>
  2. <artifactId>spring-boot-starter-web</artifactId>
  3. <exclusions>
  4. <exclusion>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-tomcat</artifactId>
  7. </exclusion>
  8. </exclusions>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-jetty</artifactId>
  13. </dependency>

客户端使用

配置请求响应类

  1. package com.example.demo.jersey;
  2. public class ClientResponse<T> {
  3. private int status;
  4. private T entity;
  5. private String message;
  6. private ClientResponse() {
  7. }
  8. private ClientResponse(int status, T entity) {
  9. this.status = status;
  10. this.entity = entity;
  11. }
  12. private ClientResponse(String message, int status) {
  13. this.status = status;
  14. this.message = message;
  15. }
  16. public int getStatus() {
  17. return this.status;
  18. }
  19. public void setStatus(int status) {
  20. this.status = status;
  21. }
  22. public T getEntity() {
  23. return this.entity;
  24. }
  25. public void setEntity(T entity) {
  26. this.entity = entity;
  27. }
  28. public String getMessage() {
  29. return this.message;
  30. }
  31. public void setMessage(String message) {
  32. this.message = message;
  33. }
  34. public static <T> ClientResponse successResponse(T entity) {
  35. return new ClientResponse( 200,entity);
  36. }
  37. public static <T> ClientResponse errorResponse(String message) {
  38. return new ClientResponse(message, 400);
  39. }
  40. }

配置HttpClientUtil工具类

  1. package com.example.demo.jersey;
  2. import javax.ws.rs.client.Client;
  3. import javax.ws.rs.client.ClientBuilder;
  4. import javax.ws.rs.client.Entity;
  5. import javax.ws.rs.client.WebTarget;
  6. import javax.ws.rs.core.MediaType;
  7. import javax.ws.rs.core.Response;
  8. public class HttpClientUtil {
  9. private Client client;
  10. private String baseUrl;
  11. private WebTarget webTarget;
  12. public enum HttpMethod {
  13. //get
  14. GET("GET"),POST("POST"),PUT("PUT"),DELETE("DELETE");
  15. String method;
  16. HttpMethod(String method){
  17. this.method = method;
  18. }
  19. }
  20. private HttpClientUtil(String baseUrl){
  21. this.client = ClientBuilder.newClient();
  22. this.baseUrl = baseUrl;
  23. this.webTarget = this.client.target(this.baseUrl);
  24. }
  25. public static <T> ClientResponse httpRequest(String url , HttpMethod httpMethod, Object params, Class<T> classType){
  26. HttpClientUtil httpClientUtil = null;
  27. Response response = null;
  28. try {
  29. httpClientUtil = new HttpClientUtil(url);
  30. switch (httpMethod) {
  31. case GET:
  32. response = httpClientUtil.webTarget.request().get();
  33. break;
  34. case POST:
  35. response = httpClientUtil.webTarget.request().post(Entity.entity(params, MediaType.APPLICATION_JSON_TYPE));
  36. break;
  37. case PUT:
  38. response = httpClientUtil.webTarget.request().put(Entity.entity(params, MediaType.APPLICATION_JSON_TYPE));
  39. break;
  40. case DELETE:
  41. response = httpClientUtil.webTarget.request().delete();
  42. break;
  43. default:
  44. response = httpClientUtil.webTarget.request().get();
  45. }
  46. if (response.getStatus() != 200) {
  47. return ClientResponse.errorResponse("Failed with HTTP error code : " + response.getStatus());
  48. } else {
  49. return ClientResponse.successResponse(response.readEntity(classType));
  50. }
  51. } finally {
  52. if (response != null){
  53. response.close();
  54. }
  55. if (httpClientUtil != null){
  56. httpClientUtil.close();
  57. }
  58. }
  59. }
  60. public void close() {
  61. this.client.close();
  62. }
  63. }

测试

  1. import java.util.*;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. String url = "http://127.0.0.1:8056/heren/api/bill/pat-master-index/patient-id?patientId=11105298";
  5. ClientResponse response = HttpClientUtil.httpRequest(url, HttpClientUtil.HttpMethod.GET,null, Map.class);
  6. if (response.getStatus() == 200){
  7. Map map = (Map) response.getEntity();
  8. System.out.println(map.get("name"));
  9. } else {
  10. System.out.println(response.getMessage());
  11. }
  12. }
  13. }