/ ** * extract a common class * * @author an xz * * / @Service public class ApiService { @Autowired Private RequestConfig config; @Autowired Private CloseableHttpClient httpClient; / ** * no arguments get request * * @param URL * @return * / public String the doGet (String URL) { // HttpGet objects HttpGet GET = new new HttpGet (URL); get.setConfig (config); CloseableHttpResponse Response = null; try { response = httpClient.execute(get); if (response != null && response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(response.getEntity(), "utf-8"); } } catch (IOException e) { e.printStackTrace(); } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } /** * 有参的get请求 * * @param url * @return */ public String doGet(String url, Map<String, Object> params) { List<NameValuePair> nvprs = new ArrayList<>(); // 遍历map for (String key : params.keySet()) { NameValuePair nvpr = new BasicNameValuePair(key, params.get(key).toString()); nvprs.add(nvpr); } CloseableHttpResponse response = null; try { URI uri = new URIBuilder(url).addParameters(nvprs).build(); // HttpGet对象 HttpGet get = new HttpGet(uri); get.setConfig(config); response = httpClient.execute(get); if (response != null && response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(response.getEntity(), "utf-8"); } } The catch (Exception E) { e.printStackTrace (); } the finally { IF (Response =! Null ) { the try { response.close (); } the catch (IOException E) { e.printStackTrace (); } } } return null ; } // the Save Update the Delete (different status return value is not the same and some have and some do not return value return value?.) public the Result doPost (String url, the Map <String, Object> params) { List<NameValuePair> nvprs = new ArrayList<>(); // 遍历map for (String key : params.keySet()) { NameValuePair nvpr = new BasicNameValuePair(key, params.get(key).toString()); nvprs.add(nvpr); } CloseableHttpResponse response = null; try { URI uri = new URIBuilder(url).addParameters(nvprs).build(); // HttpGet对象 HttpPost post = new HttpPost(uri); post.setConfig(config); response = httpClient.execute(post); return new Result(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "utf-8")); } catch (Exception e) { e.printStackTrace(); } finally { if (response != null) { try { response.close(); } catch(IOException E) { e.printStackTrace(); } } } . the Result (response.getStatusLine () getStatusCode (),Return null ; } // the Save the Update the Delete (status return values are not the same and some different return value does not return any value?.) Public the Result the doPost (String URL) { CloseableHttpResponse Response = null ; the try { // HttpGet Object HttpPost = POST new new HttpPost (URL); post.setConfig (config); Response = httpClient.execute (POST); return new new EntityUtils.toString (response.getEntity (), "UTF-. 8")); } catch (Exception e) { e.printStackTrace(); } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } }
The method of application of the above example
import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.taotao.manage.pojo.Item; @Service public class ItemService { @Autowired private ApiService apiService; private ObjectMapper mapper=new ObjectMapper(); public Item iteminfo(Long itemId) { Map<String,Object> params=new HashMap<String,Object>(); params.put("itemId", itemId); String data=apiService.doGet("http://manage.taotao.com/rest/item/queryId",params); System.out.println(data); if(StringUtils.isNotBlank(data)) { try{ // when the package entity classes Item Item = mapper.readValue (Data, Item. Class ); return Item; } the catch (IOException E) { e.printStackTrace (); } return null ; } return null ; } }