By Marone: July 2016
REST API with jersey using
PUT POST DELETE
Goal
In my last article I built a simple hello world Rest service, now i want to introduce more examples trying to light up various aspects of restful service.Used technologies
JDK 1.8Maven 3.2
Maven dependencies: jersey 1.18. + jackson.jaxrs + jackson-xc + junit
pom.xml
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.18.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
Model: Tutorial
Resource: TutorialResource
Where are HEAD and OPTIONS methods!
By default the JAX-RS runtime will automatically support the methods HEAD and OPTIONS, if not explicitly implemented. For HEAD the runtime will invoke the implemented GET method (if present) and ignore the response entity (if set). A response returned for the OPTIONS method depends on the requested media type defined in the 'Accept' header. The OPTIONS method can return a response with a set of supported resource methods in the 'Allow' header or return a WADL document.
By default the JAX-RS runtime will automatically support the methods HEAD and OPTIONS, if not explicitly implemented. For HEAD the runtime will invoke the implemented GET method (if present) and ignore the response entity (if set). A response returned for the OPTIONS method depends on the requested media type defined in the 'Accept' header. The OPTIONS method can return a response with a set of supported resource methods in the 'Allow' header or return a WADL document.
Rest Starter
Call the url
To obtain all tutorials: http://localhost:10080/api/tutorialsJust only one specific tutorial: http://localhost:10080/api/tutorials/2
Using curl
curl -i http://localhost:10080/api/tutorials
curl -i http://localhost:10080/api/tutorials/1
curl -H "Content-Type: application/json" -X POST -d '{"author":"Adam snake","id":"1","name":"Python Basics"}' http://localhost:10080/api/tutorials
curl -H "Content-Type: application/json" -X PUT -d '{"author":"Adam changed","id":"1","name":"Python Basics"}' http://localhost:10080/api/tutorials
curl -X DELETE http://localhost:10080/api/tutorials/1
curl -X HEAD http://localhost:10080/api/tutorials/1
curl -X OPTIONS http://localhost:10080/api