API consuming is the best, quickest and the most reliable way to get real names. The API is located here:
Usually the process of working via an API has two stages:Nowadays there are lots of different tools (libraries, frameworks, DSLs) to get data via API. Here is the short list of the most popular tools:
Please note, that if you want to know about how to use some other library/framework to consume API, please use a feedback form on the about page to let me know.
Jersey and Jackson is a perfect bundle, where Jersey is used as a transport tool and Jackson is a wat to serialize/deserialize request/response to the convenient form for further processing. Of course, it is used not for client creation only.
To use Jersey with Jackson first you need to create class where the data, received in response will be applied to (mapped). Every field should be mentioned in the model and each field should have its getter and setter (setters aren't required here, as we'll set nothing, but they should be present). Method, that prints the object (toString()) should be overridden
public class RealNamesModel {
private String firstName;
private String lastName;
private String fullName;
private String timeTaken;
public RealNamesModel(){}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getTimeTaken() {
return timeTaken;
}
public void setTimeTaken(String timeTaken) {
this.timeTaken = timeTaken;
}
@Override
public String toString() {
return "RealNamesModel{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", fullName='" + fullName + '\'' +
'}';
}
public class RealNamesTest{
private final String URI = "https://gentle-journey-86001.herokuapp.com/real-names/generate";
private RealNamesModel realNamesModel = new RealNamesModel();
@Test
public void getRealNames(){
//Client initialization
Client client = ClientBuilder.newClient();
//Target initialization and pointing client
WebTarget target = client.target(URI);
//preparing response
Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
.accept(MediaType.APPLICATION_JSON_TYPE).get();
//Mapping the response object to given class
realNamesModel = response.readEntity(RealNamesModel.class);
//Printing the whole object
System.out.println(realNamesModel.toString());
//Getting each value separately
System.out.println(realNamesModel.getFirstName());
System.out.println(realNamesModel.getLastName());
System.out.println(realNamesModel.getFullName());
}
}
The fully working example is available on Github: real-names-jersey-jackson-example