Created
September 13, 2016 18:48
-
-
Save rgiaviti/e6917b46b05725d7001a8b6967699c75 to your computer and use it in GitHub Desktop.
Simple REST Client using Jersey
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The MIT License (MIT) | |
* Copyright (c) 2016 Ricardo Giaviti | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of | |
* this software and associated documentation files (the "Software"), to deal in | |
* the Software without restriction, including without limitation the rights to | |
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
* the Software, and to permit persons to whom the Software is furnished to do so, | |
* subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
package br.com.restclient; | |
import javax.ws.rs.client.Client; | |
import javax.ws.rs.client.ClientBuilder; | |
import javax.ws.rs.client.Entity; | |
import javax.ws.rs.client.Invocation; | |
import javax.ws.rs.core.MultivaluedMap; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.core.UriBuilder; | |
/** | |
* Simple example to demonstrate how to use Jersey to build a Simple REST client. | |
* Jersey is a lot more powerful. Take a look at the full documentation. | |
* https://jersey.java.net/ | |
* | |
* @author Ricardo Giaviti - https://github.com/rgiaviti | |
* | |
* Maven Dependency needed: | |
* <dependency> | |
* <groupId>org.glassfish.jersey.core</groupId> | |
* <artifactId>jersey-client</artifactId> | |
* <version>2.23.2</version> | |
* </dependency> | |
*/ | |
public class SimpleRestClient { | |
/** | |
* Target (URL) of API we are calling | |
* Example: http://example.com/api | |
*/ | |
private transient String target; | |
/** | |
* Constructor with target (URL format). | |
* | |
* @param target URL of API. Example: http://example.com/api | |
*/ | |
public SimpleRestClient(final String target) { | |
this.target = target; | |
} | |
/** | |
* Do a POST request and return the Response. The object Response contains all the data you | |
* might need to check the information returned (codes, body...). | |
* | |
* @param path /v1/resource/item | |
* @param content the content you want to put in the request body. | |
* @param headers the HTTP headers | |
* @return | |
*/ | |
public Response doPost(final String path, final Entity<?> content, final MultivaluedMap<String, Object> headers) { | |
return this.buildRequest(path, headers).buildPost(content).invoke(); | |
} | |
/** | |
* Do a GET request and return the Response. The object Response contains all the data you | |
* might need to check the information returned (codes, body...). | |
* | |
* @param path /v1/resource/item | |
* @param headers the HTTP headers | |
* @return | |
*/ | |
public Response doGet(final String path, final MultivaluedMap<String, Object> headers) { | |
return this.buildRequest(path, headers).buildGet().invoke(); | |
} | |
/** | |
* Do a PUT request and return the Response. The object Response contains all the data you | |
* might need to check the information returned (codes, body...). | |
* | |
* @param path the REST path. Example: /v1/resource/item | |
* @param content the content you want to put in the request body. | |
* @param headers the HTTP headers | |
* @return | |
*/ | |
public Response doPut(final String path, final Entity<?> content, final MultivaluedMap<String, Object> headers) { | |
return this.buildRequest(path, headers).buildPut(content).invoke(); | |
} | |
/** | |
* Do a DELETE request and return the Response. The object Response contains all the data you | |
* might need to check the information returned (codes, body...). | |
* | |
* @param path the REST path. Example: /v1/resource/item | |
* @param headers The HTTP Headers | |
* @return the response object | |
*/ | |
public Response doDelete(final String path, final MultivaluedMap<String, Object> headers) { | |
return this.buildRequest(path, headers).buildDelete().invoke(); | |
} | |
/** | |
* Build the Request for all other methods. Acts like a helper | |
* | |
* @param path | |
* @param headers | |
* @return | |
*/ | |
private Invocation.Builder buildRequest(final String path, final MultivaluedMap<String, Object> headers) { | |
final Client client = ClientBuilder.newClient(); | |
final UriBuilder uriBuilder = UriBuilder.fromUri(this.target); | |
return client.target(uriBuilder).path(path).request().headers(headers); | |
} | |
/** | |
* Return the target | |
* | |
* @return the target URL | |
*/ | |
public String getTarget() { | |
return target; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment