ATS : REST Operations - Supported HTTP methods

Following is a list with currently supported HTTP methods


GET method

GET is used to retrieve an already existing resource on the server without modifying it

// execute a GET method
RestResponse response = client.get();
 
// now examine the response



POST method

POST is used to create a new resource on the server

POST a form

Post a form containing some information about some person:

// POST a form
client.postForm( new RestForm().addParameter( "firstName", USER_FIRST_NAME )
                               .addParameter( "lastName", USER_LAST_NAME )
                               .addParameter( "age", "40" ) );

POST a java object

Post some java object:

// prepare the object to POST
Person person = new Person();
person.setFirstName( "Indiana" );
person.setLastName( "Johnes" );
person.setAge( 40 );

// Optionally specify the request and/or response media types
client.setRequestMediaType( RestMediaType.APPLICATION_XML );
client.setResponseMediaType( RestMediaType.APPLICATION_XML );

// POST the object
RestResponse response = client.postObject( person );

In the example above, Person is some particular class provided by you, not the ATS framework.



PUT method

PUT is used to update an already existing resource on the server

PUT a form

Equivalent to the POST form example, but use the putForm method instead

PUT a java object

Equivalent to the POST object example, but use the putObject method instead



DELETE method

DELETE is used to delete an already existing resource on the server.

// make an instance of the REST client by specifying the target URI
RestClient client = new RestClient( <target URI>/people/delete/123 );

// execute the delete method
RestResponse response = client.delete();

// read the body of the response
String responseBody = response.getBodyAsString();

In this example we delete a person with id 123 which is specified as part of the URI.

Of course there are different ways of pointing the resource to delete, for example you can use a request parameter.



Any method

There are other HTTP methods not supported by our REST client, there could be also some methods specific only to your server. Here is how you can proceed in such case:

// make an instance of the REST client by specifying the target URI
RestClient client = new RestClient( <target URI> );

// execute some HTTP method
RestResponse response = client.execute( "KICK" );

// read the body of the response
String responseBody = response.getBodyAsString();

In this case our server knows how to handle a method called KICK (smile)



Non-compliant method execution

There are cases, where some method invocations are not compliant. Example of such method invocations are:

  • PUT with null body
    • Exception - java.lang.IllegalStateException: Entity must not be null for http method PUT
  • DELETE with body
    • Exception - java.lang.IllegalStateException: Entity must be null for http method DELETE.

To suppress such errors (make then log a warning message instead),  use the following snippet:

// make an instance of the REST client by specifying the target URI
RestClient client = new RestClient( <target URI> );

// make instance of REST client configurator
RestClientCondigurator restClientConfigurator = new RestClientConfigurator();
// suppress non-compliant method invocation exceptions
restClientConfigurator.setProperty(org.glassfish.jersey.client.ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);

// set configurator to the client, BEFORE executing any REST/HTTP method
client.setClientConfigurator(restClientConfigurator);

// execute some non-compliant HTTP method
// now the next line will not throw any exceptions, but will gracefully log a warning instead
RestResponse response = client.execute( "DELETE", "{\"id\":1}" );

// read the body of the response
String responseBody = response.getBodyAsString();



Back to parent page

Go to Table of Contents