JAX-RS:客户端的几种方式

HttpURLConnection

public static byte[] request(String urlString, String requestData, String method, String contentType, int timeout)
        throws WsException {
    byte[] responseData = null;

    HttpURLConnection con = null;
    BufferedReader in = null;
    PrintWriter out = null;

    try {
        URL url = new URL(urlString);
        con = (HttpURLConnection) url.openConnection();
        con.setInstanceFollowRedirects(false);
        con.setRequestMethod(method); // 置为POST方法
        con.setRequestProperty(CONTENT_TYPE, contentType);
        con.setDoInput(true); // 开启输入流
        con.setDoOutput(true); // 开启输出流
        con.setUseCaches(false);

        //set default timeout
        if (timeout == 0) {
            timeout = TIMEOUT;
        }
        con.setConnectTimeout(timeout);
        con.setReadTimeout(timeout);

        // 如果请求数据不为空,输出该数据。
        if (requestData != null) {
            out = new PrintWriter(new OutputStreamWriter(con.getOutputStream(), CHARACTER_ENCODING));
            out.write(URLEncoder.encode(requestData, CHARACTER_ENCODING));
            out.flush();
        }

        int length = con.getContentLength();
        // 如果回复消息长度不为-1,读取该消息。
        if (length != -1) {
            DataInputStream dis = new DataInputStream(con.getInputStream());
            responseData = new byte[length];
            dis.readFully(responseData);
            dis.close();
        }
    } catch (SocketTimeoutException e) {
        String errorMsg = "[SocketTimeoutException]request:url[" + urlString + "] request:[" + requestData
                + "] timout:[" + timeout + "]  msg=" + e.getMessage();
        LOG.error(errorMsg);
        throw new WsException("SocketTimeoutException", e.getMessage());
    } catch (MalformedURLException e) {
        String errorMsg = "[MalformedURLException]request:url[" + urlString + "]  request[" + requestData
                + "]  msg=" + e.getMessage();
        LOG.error(errorMsg);
        throw new WsException("MalformedURLException", e.getMessage());
    } catch (IOException e) {
        String errorMsg = "[IOException]request:url[" + urlString + "]  request[" + requestData + "]  msg="
                + e.getMessage();
        LOG.error(errorMsg);
        throw new WsException("IOException", e.getMessage());
    } finally {
        if (out != null) {
            out.flush();
        }
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
        if (con != null) {
            con.disconnect();
        }
    }

    return responseData;
}

 

HttpClient

public static String req(String url, String jsonData, String token) throws WsException {
    PostMethod post = new PostMethod(url);
    RequestEntity requestEntity;
    String bodyContent;
    try {
        requestEntity = new StringRequestEntity(jsonData, CONTENT_JSON, CHARACTER_ENCODING);
        post.setRequestEntity(requestEntity);
        post.addRequestHeader("Accept", CONTENT_JSON);
        if (StringUtils.isNotBlank(token)) {
            post.setRequestHeader("Authorization", token);
        }

        HttpClient httpClient = new HttpClient();
        httpClient.executeMethod(post);
        if (post.getStatusCode() != 200) {
            throw new RuntimeException(post.getStatusCode() + "");
        }
        bodyContent = post.getResponseBodyAsString();
    } catch (UnsupportedEncodingException e) {
        String errorMsg = "[UnsupportedEncodingException]request:url[" + url + "]  request[" + jsonData + "]  msg="
                + e.getMessage();
        LOG.error(errorMsg);
        throw new WsException("UnsupportedEncodingException", e.getMessage());
    } catch (HttpException e1) {
        String errorMsg = "[HttpException]request:url[" + url + "]  request[" + jsonData + "]  msg="
                + e1.getMessage();
        LOG.error(errorMsg);
        throw new WsException("HttpException", e1.getMessage());
    } catch (IOException e2) {
        String errorMsg = "[IOException]request:url[" + url + "]  request[" + jsonData + "]  msg="
                + e2.getMessage();
        LOG.error(errorMsg);
        throw new WsException("IOException", e2.getMessage());
    } finally {
        post.releaseConnection();
    }
    return bodyContent;
}

WebClient

WebClient client = WebClient.create("http://books");
client.path("bookstore/books");
client.type("text/xml").accept("text/xml")
Response r = client.post(new Book());
Book b = r.readEntity(Book.class);

 

更多信息这里

你可能感兴趣的:(Restful,jaxrs,webclient)