JAX-RS - @PUT Example

[Last Updated: Jun 22, 2017]

The difference between POST and PUT is: POST creates resources without defining the new resource path. Whereas, PUT creates new resources by defining complete resource path. Essentially when we are creating a new resource via PUT, we are requesting the server to create the new resource at the requested URI.

Post is submitted against resource URI like Please see our post example. PUT is submitted against URI like where item with id 10 is to be created as a new resource.

JAX-RS service method should be annotated with @PUT:

 public Response createItem(Item item, @PathParam("sku") String sku) {
  if (dataService.itemExists(sku)) {
        return Response.noContent()
     } else {
        dataService.createItem(item, sku);
        //status code 201
         return Response.created(uriInfo.getAbsolutePath())

Note above method contains a sub resource path via @Path("{sku}"), which is usual for a PUT method.

Here's the client code requesting a PUT:

Item item = ...
Client client = ClientBuilder.newClient();
WebTarget target ="");
String response = target.path("10")
                 .put(Entity.entity(item, MediaType.APPLICATION_JSON));;

Example Project

Example Project

Dependencies and Technologies Used:

  • jersey-server 2.22.1: Jersey core server implementation.
  • jersey-container-servlet 2.22.1: Jersey core Servlet 3.x implementation.
  • jersey-media-moxy 2.22.1: Jersey JSON entity providers support module based on EclipseLink MOXy.
  • jersey-test-framework-provider-jdk-http 2.22.1: Jersey Test Framework - JDK HTTP container.
  • JDK 1.8
  • Maven 3.3.9

Jxrs Put Example Select All Download
  • jaxrs-put-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • api
        • test
          • java
            • com
              • logicbig
                • example

    To test the application run the JUnit test ItemRestServiceTest. Or you can also create a standalone client using JAX-RS Client API. The test class also uses the same client API.

    To run the application, run embedded tomcat server (not needed if you just want to run the JUnit test):

    d:\jaxrs-put-example>mvn clean install tomcat7:run-war -DskipTests

    See Also