Jersey HTTP Post Example

Earlier I have shared with you how to use JAX-RS to access HTTP GET Request and how extract URI Query Parameters and how to read Path Parameters. In this blog post I am going to share with you how to handle HTTP POST request and how to read the content of HTTP Request Body as well as how to Form Parameters with the @FormParam annotation.

I assume you already have your JAX-RS application created but if you do not, please follow this example on How to create a new Jersey JAX-RS project with Maven.

HTTP POST Request Body JSON Document

Let’s assume we are working on a web service that needs to access JSON document containing user details to be recorded in database. The user details will be sent as HTTP POST request in a form of JSON document like the one below. Of course you can extend this JSON document and add additional details into it.

{
 "firstName":"Sergey",
 "lastName":"Kargopolov"
}

User Profile Java Model Class

In our Java code to map the above JSON document to a Java object we will need to create a model class called UserProfile like the one below:

package com.appsdeveloperblog.examples.http.post;

import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class UserProfile {
    private String firstName;
    private String lastName;

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Java Method with @Post Annotation

For our web service method to be able to accept HTTP Post request we will need to create a method that accepts UserProfile object as an argument and annotate this method with @Post annotation. Like the one below:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public UserProfile createUserProfile(UserProfile userProfile) {

    //we can make use of UserProfile now
    String firstName = userProfile.getFirstName();
    String lastName = userProfile.getLastName();
    
    System.out.println("First name = " + firstName);
    System.out.println("Last name = " + lastName);

   // And when we are done we can return user profile back
    return userProfile;
}

Now let’s add this method to a Root Resource Class to look at a complete example:

Root Resource Class with @Path Annotation

package com.appsdeveloperblog.examples.http.post;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
 
@Path("/users")
public class HttpPostExample {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public UserProfile createUserProfile(UserProfile userProfile) {

        //we can make use of UserProfile now
        String firstName = userProfile.getFirstName();
        String lastName = userProfile.getLastName();
        
        System.out.println("First name = " + firstName);
        System.out.println("Last name = " + lastName);

       // And when we are done we can return user profile back
        return userProfile;
    }
}

Please note the @Path(“/users”) annotation. This annotation makes the Root Resource class be available under the path /users . 

To make one of our Web Service methods handle HTTP POST request we need to mark it with the @Post annotation. Please note the @POST annotation above the  createUserProfile method. When HTTP POST request is sent to /users the createUserProfile will be called and the JSON document with the user details I have mentioned above will be converted into a Java object of UserProfile type.

I hope this short blog post was of some value to you. Check out the below video courses which are about building RESTful Web Services with Java. I am confident you will find a lot of value in them and your learning progress will greatly improve.

Happy learning! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *