# httputil **Repository Path**: dreamlikeocean/httputil ## Basic Information - **Project Name**: httputil - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2020-09-12 - **Last Updated**: 2021-11-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # httputil使用说明 ### 非SpringBoot环境 即可 ``` public void test1() { Test test = new HttpInterfaceProxyGenerator() .getProxy(Test.class); test.get(); } interface Test{ @Request(baseUri = "http://www.baidu.com") String get(); } ``` ### SpringBoot环境 启动类添加扫描注解,指定扫描包,不指定则默认为启动类所在文件及其子文件下(与mybatis一致) ```java @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @HttpComponentScanner(basePackages = "") public class HttpClientApplication { public static void main(String[] args){ SpringApplication.run(HttpClientApplication.class, args); } } ``` 需要被实现的接口添加注解@HttpComponet即可 ```java @HttpComponent public interface HttpTest { @Request(baseUri = "http://localhost:8080/headers", responseContentType = ContentType.APPLICATION_FORM_URLENCODED) A get(@HeaderParam("dreamlike") String a, @RequestParam String b, @HeaderParam String c); @Request(baseUri = "http://localhost:8080/post", requestContentType = ContentType.APPLICATION_JSON,method = RequestMethod.POST) List post(@RequestBody A a); } ``` 如同mybatis的mapper一样,使用@AutoWired注入 idea会出现红线,不用管 ```java @Autowired private HttpTest h; ``` ### 注意事项 1,只支持文本类型返回值 2,返回值支持类似于List,Future>,Map的单层泛型 3,异步只支持为Future类型或者CompletableFuture类型,因为本质上就是CompletableFuture类型 ### 注解释义 #### @Request @Request 标识在接口方法上 异步使用的线程池名称executorName,其的解析在HttpDefinitionGenerator的构造方法中的Map executorMap指定,默认为forkjoin的线程池 ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Request { String baseUri(); RequestMethod method() default RequestMethod.GET; boolean isAsync() default false; String requestContentType() default ContentType.APPLICATION_JSON; String responseContentType() default ContentType.APPLICATION_JSON; int timeoutSeconds() default 10; HttpClient.Version version() default HttpClient.Version.HTTP_1_1; String executorName() default HttpDefinitionGenerator.DEFAULT_EXECUTOR_NAME; } ``` #### @HeaderParam @HeaderParam标识在方法参数上 其value()为header的key,实参为header的value value若为空则取参数名作为key ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface HeaderParam { String value() default ""; } ``` #### @RequestBody 与@RequestParam 不共存,在post和put方法里面会判断是否存在@RequestBody,若存在会直接序列化@RequestBody标识的参数。而且默认是最后一个出现的@RequestBody 标识在方法参数上,根据@Request的requestContentType()值决定被标注的参数如何序列化,并将其放入请求体 其中urlencoded默认实现是由被标注的参数实体类中字段名作为key,其实际值作为value json默认实现为jackson序列化 #### @RequestParam 与@RequestBody不共存,在post和put方法里面会判断是否存在@RequestBody,若存在会直接序列化@RequestBody标识的参数。而且默认是最后一个出现的@RequestBody 标识在方法参数上,根据@Request的method()决定如何作用 若为get,delete方法则将参数直接根据值拼接到url后,其value()为key,实参调用toString()的返回值为value,value若为空则取参数名作为key 若为post,put方法则按照requestContentType()进行处理 urlencoded则与get方法中的处理一致 json则是收集为一个Map>再序列化(主要是为了防止key重复)