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);

 
詳細ここ