| | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * Rest服务帮助类 |
| | | * @author WWW |
| | | */ |
| | | @SuppressWarnings("ALL") |
| | | public class RestHelper { |
| | | private static RestTemplate restTemplate; |
| | | |
| | | private final static Log log = LogFactory.getLog(RestHelper.class); |
| | | |
| | | /** |
| | | * 获取RestTemplate |
| | | * |
| | | * @return RestTemplate |
| | | */ |
| | | public static RestTemplate getRestTemplate() { |
| | | if (restTemplate == null) { |
| | | restTemplate = SpringContextHelper.getBean(RestTemplate.class); |
| | |
| | | return restTemplate; |
| | | } |
| | | |
| | | /** |
| | | * Get请求-HttpURLConnection |
| | | * |
| | | * @param url URL地址 |
| | | * @return 字符串 |
| | | * @throws IOException IO异常 |
| | | */ |
| | | public static String getForConn(String url) throws IOException { |
| | | BufferedReader br = null; |
| | | HttpURLConnection conn = null; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Post请求-HttpURLConnection |
| | | * |
| | | * @param url URL地址 |
| | | * @param query 查询条件 |
| | | * @return 字符串 |
| | | * @throws IOException IO异常 |
| | | */ |
| | | public static String postForConn(String url, String query) throws IOException { |
| | | BufferedReader br = null; |
| | | HttpURLConnection conn = null; |
| | |
| | | URL restUrl = new URL(url); |
| | | |
| | | conn = (HttpURLConnection) restUrl.openConnection(); |
| | | // POST,GET,PUT,DELETE |
| | | conn.setRequestMethod("POST"); |
| | | conn.setRequestProperty("Content-Type", "application/json"); |
| | | conn.setDoOutput(true); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Get请求-CloseableHttpClient |
| | | * |
| | | * @param uri Uri地址 |
| | | * @return 响应字符串 |
| | | */ |
| | | public static String get(String uri) { |
| | | try { |
| | | CloseableHttpClient httpClient = HttpClients.custom().build(); |
| | |
| | | HttpGet httpGet = new HttpGet(uri); |
| | | |
| | | CloseableHttpResponse closeResponse = httpClient.execute(httpGet); |
| | | // 取出返回体 |
| | | |
| | | HttpEntity entity = closeResponse.getEntity(); |
| | | |
| | | return EntityUtils.toString(entity, StaticData.TEXT_ENCODER); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Post请求-CloseableHttpClient |
| | | * |
| | | * @param uri Uri地址 |
| | | * @param postData 待发送数据 |
| | | * @return 响应字符串 |
| | | */ |
| | | public static String post(String uri, List<NameValuePair> postData) { |
| | | try { |
| | | CloseableHttpClient httpClient = HttpClients.custom().build(); |
| | |
| | | |
| | | CloseableHttpResponse closeResponse = httpClient.execute(httpPost); |
| | | |
| | | // 取出返回体 |
| | | HttpEntity entity = closeResponse.getEntity(); |
| | | |
| | | return EntityUtils.toString(entity, StaticData.TEXT_ENCODER); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取错误信息 |
| | | * |
| | | * @param uri Uri地址 |
| | | * @param ex 异常 |
| | | * @return 错误信息 |
| | | */ |
| | | public static String getErrorInfo(String uri, Exception ex) { |
| | | Map<String, Object> map = new LinkedHashMap<>(); |
| | | map.put("result", null); |
| | |
| | | return map.toString(); |
| | | } |
| | | |
| | | /** |
| | | * GET请求(REST) |
| | | */ |
| | | public static String getForRest(String uri) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.getForObject(uri, String.class); |
| | | } |
| | | |
| | | /** |
| | | * GET请求(REST) |
| | | */ |
| | | public static <T> T getForRest(String uri, Class<T> clazz) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.getForObject(uri, clazz); |
| | | } |
| | | |
| | | /** |
| | | * POST请求(REST) |
| | | */ |
| | | public static String postForRest(String uri, Map<String, Object> map) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.postForObject(uri, map, String.class); |
| | | } |
| | | |
| | | /** |
| | | * POST请求(REST) |
| | | */ |
| | | public static <T> String postForRest(String uri, List<T> list) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.postForObject(uri, list, String.class); |
| | | } |
| | | |
| | | /** |
| | | * POST请求(REST) |
| | | */ |
| | | public static <T> String postForRest(String uri, T t) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.postForObject(uri, t, String.class); |
| | | } |
| | | |
| | | /** |
| | | * DELETE请求(REST) |
| | | */ |
| | | public static void deleteForRest(String uri) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | rest.delete(uri); |
| | | } |
| | | |
| | | public static void deleteForRest(String uri, Map<String, Object> map) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | rest.delete(uri, map); |
| | | } |
| | | } |