Skip to content

一个封装jdk httpclient的框架,使其使用更加灵活方便

License

Notifications You must be signed in to change notification settings

fishlikewater/raiden-http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JDK 原生httpclient封装工具

使用方式

  • 使用spring boot

    • 引入raiden-spring-boot-starter
    • 添加扫描路径
    @SpringBootApplication
    @HttpScan("com.github.fishlikewater.test")  // 这里添加扫描路径
    public class RaidenSpringBootTestApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(RaidenSpringBootTestApplication.class, args);
        }
    
    }
    • 定义接口
    @HttpServer(sourceHttpClient = "customer") //customer 为自定义注册的httpclient
    @Interceptor(MyInterceptor.class ) // 这里定义改接口类下所有接口的拦截器
    public interface DemoRemote {
    
        /**
         * 测试访问百度 如不加协议 默认是http
         *
         * @return {@code String}
         */
        @GET("www.baidu.com")
        String baidu();
    
        /**
         * 测试访问百度 https
         *
         * @return {@code String}
         */
        @GET("https://www.baidu.com")
        String baidu2();
    }
    • 自定义注册的httpclient (系统会默认注册一个名为defaulthttpclient
    @Component
    public class CustomerHttpClient implements SourceHttpClientRegister {
        @Override
        public void register(SourceHttpClientRegistry registry) {
            SSLContext ctx;
            try {
                ctx = SSLContext.getInstance("TLS");
                X509TrustManager tm = new DefaultTrustManager();
                ctx.init(null, new TrustManager[]{tm}, null);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            HttpClient customer = HttpClient.newBuilder()
                    .sslContext(ctx)
                    .connectTimeout(Duration.ofSeconds(60))
                    .version(HttpClient.Version.HTTP_1_1)
                    .build();
            registry.register("customer", customer);
        }
    }
    • 定义拦截器
    @Component
    public class MyInterceptor implements HttpInterceptor {
    
      @Override
      public Response intercept(Chain chain) throws IOException, InterruptedException {
          System.out.println("------interceptor------");
          return chain.proceed();
      }
    
      @Override
      public int order() {
          return 0;
      }
    }
  • 单独使用

    • 引入raiden-core即可

    • 1.定义接口使用

      • 定义接口
      @HttpServer(sourceHttpClient = "third") //customer 为自定义注册的httpclient
      @Interceptor(MyInterceptor.class ) // 这里定义改接口类下所有接口的拦截器
      public interface DemoRemote {
      
          /**
           * 测试访问百度 如不加协议 默认是http
           *
           * @return {@code String}
           */
          @GET("www.baidu.com")
          String baidu();
      
          /**
           * 测试访问百度 https
           *
           * @return {@code String}
           */
          @GET("https://www.baidu.com")
          String baidu2();
      }
      • 调用接口
      public class RemoteTest {
      
          @Before
          public void before() throws ClassNotFoundException {
              HttpBootStrap.setSelfManager(true);
              HttpBootStrap.init("com.github.fishlikewater.raidencore.remote");
              HttpBootStrap.registerHttpClient("third", HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build());
              HttpBootStrap.getLogConfig().setEnableLog(false).setLogLevel(LogConfig.LogLevel.BASIC);
          }
      
          @Test
          public void test() {
              DemoRemote remote = HttpBootStrap.getProxy(DemoRemote.class);
              String s = remote.baidu();
              Assert.assertNotNull(s);
          }
      
      }
    • 2.直接使用

      public class OnlyHttpRequestClientTest {
      
          @Before
          public void before() {
              HttpBootStrap.init();
              HttpBootStrap
                      .getLogConfig()
                      .setLogLevel(LogConfig.LogLevel.HEADS)
                      .setEnableLog(true);
          }
      
          @Test
          public void testClient() throws InterruptedException, IOException {
              HttpRequestClient httpRequestClient = HttpBootStrap.getHttpRequestClient();
              String sync = httpRequestClient.getSync("https://www.baidu.com", String.class);
              Assert.assertNotNull(sync);
          }
      }

更改使用方式参考 raiden-spring-boot-test 下的使用实例

About

一个封装jdk httpclient的框架,使其使用更加灵活方便

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages