Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.vladimirbykov08.rest_client_crud;
- import com.sun.istack.NotNull;
- import com.sun.jersey.api.client.Client;
- import com.sun.jersey.api.client.ClientResponse;
- import com.sun.jersey.api.client.GenericType;
- import com.sun.jersey.api.client.WebResource;
- import javax.ws.rs.core.MediaType;
- import java.util.List;
- public class App {
- private static final String URL = "http://localhost:8585/rest/persons";
- public static void main(String[] args) {
- Client client = Client.create();
- //printList(getPersons(client, null, null, null, null, null));
- //System.out.println();
- //printList(getPersons(client, "V2","BY" , null, null, null));
- String id = create(client,"Vladimir","Bykov",24,"VB", "QA");
- System.out.println(id);
- update(client,Integer.parseInt(id),"1",null,null,null,null);
- // printList(getPersons(client, null, null, null, null, null));
- //delete(client,1);
- }
- private static List<Person> getPersons(Client client,
- String name,
- String surname,
- Integer age,
- String abbreviation,
- String job) {
- WebResource webResource = client.resource(URL);
- if (name != null) {
- webResource = webResource.queryParam("name", name);
- }
- if (surname != null) {
- webResource = webResource.queryParam("surname", surname);
- }
- if (age != null) {
- webResource = webResource.queryParam("age", age.toString());
- }
- if (abbreviation != null) {
- webResource = webResource.queryParam("abbreviation", abbreviation);
- }
- if (job != null) {
- webResource = webResource.queryParam("job", job.toString());
- }
- ClientResponse response =
- webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
- if (response.getStatus() != ClientResponse.Status.OK.getStatusCode()) {
- throw new IllegalStateException("Request failed");
- }
- GenericType<List<Person>> type = new GenericType<List<Person>>() {
- };
- return response.getEntity(type);
- }
- private static String update(Client client,
- @NotNull Integer id,
- String name,
- String surname,
- Integer age,
- String abbreviation,
- String job) {
- WebResource webResource = client.resource(URL);
- if (name != null) {
- webResource = webResource.queryParam("name", name);
- }
- if (surname != null) {
- webResource = webResource.queryParam("surname", surname);
- }
- if (age != null) {
- webResource = webResource.queryParam("age", age.toString());
- }
- if (abbreviation != null) {
- webResource = webResource.queryParam("abbreviation", abbreviation);
- }
- if (age != null) {
- webResource = webResource.queryParam("job",job);
- }
- if (id != null)
- webResource = webResource.queryParam("id", id.toString());
- ClientResponse response =
- webResource.accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class);
- System.out.println(response.getStatus());
- if (response.getStatus() != ClientResponse.Status.OK.getStatusCode()) {
- throw new IllegalStateException(response.getEntity(String.class));
- }
- GenericType<String> type = new GenericType<String>() {};
- return response.getEntity(type);
- }
- private static String delete(Client client,
- @NotNull Integer id) {
- WebResource webResource = client.resource(URL);
- if (id != null)
- webResource = webResource.queryParam("id", id.toString());
- ClientResponse response =
- webResource.accept(MediaType.APPLICATION_JSON)
- .delete(ClientResponse.class);
- if (response.getStatus() != ClientResponse.Status.OK.getStatusCode()) {
- throw new IllegalStateException(response.getEntity(String.class));
- }
- GenericType<String> type = new GenericType<String>() {};
- return response.getEntity(type);
- }
- private static String create(@NotNull Client client,
- @NotNull String name,
- @NotNull String surname,
- @NotNull Integer age,
- @NotNull String abbreviation,
- @NotNull String job) {
- WebResource webResource = client.resource(URL);
- webResource = webResource.queryParam("name", name);
- webResource = webResource.queryParam("surname", surname);
- webResource = webResource.queryParam("age", age.toString());
- webResource = webResource.queryParam("abbreviation", abbreviation);
- webResource = webResource.queryParam("job", job);
- ClientResponse response =
- webResource.accept(MediaType.APPLICATION_JSON)
- .get(ClientResponse.class);
- System.out.println(response.getStatus());
- if (response.getStatus() != ClientResponse.Status.OK.getStatusCode()) {
- throw new IllegalStateException("Request failed");
- }
- GenericType<String> type = new GenericType<String>() {};
- return response.getEntity(type);
- }
- private static void printList(List<Person> persons) {
- for (Person person : persons) {
- System.out.println(person);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement