• 通过GET方法返回定义的任意对象


    	public static <T> T getByUrl(String requestUrl, Class<T> classOfT) {
    		CloseableHttpClient httpClient = HttpClients.createDefault();
    		ObjectMapper objectMapper = new ObjectMapper();
    		objectMapper.configure(MapperFeature.AUTO_DETECT_CREATORS, true);
    		objectMapper.configure(
    				DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    		objectMapper.registerModule(new JavaTimeModule());
    		objectMapper
    				.configure(
    						com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
    						false);
    
    		T r = null;
    		try {
    			HttpGet getRequest = new HttpGet(requestUrl);
    			HttpResponse response = httpClient.execute(getRequest);
    			HttpEntity entity = response.getEntity();
    			String entityStr = EntityUtils.toString(entity, "UTF-8");
    			// System.out.println(entityStr);
    			r = objectMapper.readValue(entityStr, classOfT);
    		} catch (ClientProtocolException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return r;
    	}
    

      我当时把该方法对应的文件名命名为:HttpClientUtil

  • 相关阅读:
    java内联函数
    jvm垃圾回收
    jvm内存管理
    java进程和线程的区别
    jvm
    简单易学的SSM(Spring+SpringMVC+MyBatis)整合
    Spring之声明式事务
    SpringMVC知识点小结
    Servlet之文件的上传与下载
    java使用字节流和字符流实现文件复制
  • 原文地址:https://www.cnblogs.com/ilazysoft/p/6250784.html
Copyright © 2020-2023  润新知