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

相关文章:

  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
  • 2022-02-17
  • 2021-08-01
  • 2022-01-01
  • 2021-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-09-05
  • 2021-06-15
相关资源
相似解决方案