Skip to content
nezha edited this page Feb 24, 2018 · 1 revision

通过两三行代码就可以快速的进行http访问且接口代码更易阅读。
1.pom中引入配置:

<dependency>  
    <groupId>com.github.youzan</groupId>  
    <artifactId>http-fetch</artifactId>  
    <version>1.1.7</version>  
</dependency>

2.编写接口类:

public interface BookWormHttpApi {  
  
    /** 
     * 
     * 新增一个 拦截的chain  用于增加特定请求的token 
     * @see[BookWormTokenChain] 
     * @param file 
     * @param name 
     * @param nValue 
     * @return 
     */  
    @HttpApi(timeout = 2000, url = "http://bookworm365.com/uploadImage")  
    @BookWormApi  
    UploadFileResponseVo uploadFile(@FormParam("file") File file,  
                                    @QueryParam("name") String name,  
                                    @QueryParam("n_value") String nValue);  
  
    /** 
     * 
     * 新增一个 拦截的chain  用于增加特定请求的token 
     * @see[BookWormTokenChain] 
     * @param url 
     * @param name 
     * @param nValue 
     * @return 
     */  
    @HttpApi(timeout = 2000, url = "http://bookworm365.com/uploadImage")  
    @BookWormApi  
    UploadFileResponseVo uploadFile(@FormParam("file") URL url,  
                                    @QueryParam("name") String name,  
                                    @QueryParam("n_value") String nValue);  
  
    @HttpApi(timeout = 2000, url = "http://bookworm365.com/uploadImage")  
    @BookWormApi  
    UploadFileResponseVo uploadFile(@BeanParam @FormParam UploadFileRequestVo requestVo);  
  
    @HttpApi(timeout = 2000, url = "http://bookworm365.com/checkHeader")  
    @BookWormApi  
    String checkHeader();  
  
}  

3.编写调用类:

@Test  
public void test_upload_file_common() throws Exception {  
    HttpApiService httpApiService = new HttpApiService(new HttpApiConfiguration());  
    httpApiService.init();  
    BookWormHttpApi bookWormHttpApi = httpApiService.getOrCreateService(BookWormHttpApi.class);  
    URL url = BookWormHttpApiTest.class.getClassLoader().getResource("httpapi.xml");  
    File file = new File(url.toURI());  
    UploadFileResponseVo responseVo = bookWormHttpApi.uploadFile(file, "name", "nValue");  
    System.out.println(JSON.toJSONString(responseVo));  
  
    responseVo = bookWormHttpApi.uploadFile(new URL("http://onlz2qizd.bkt.clouddn.com/800_800.png"), "name", "nValue");  
    System.out.println(JSON.toJSONString(responseVo));  
} 
Clone this wiki locally