Newsletter

RESTful Web Services (JAX-RS) @FormParam Example

Web Services » on Jul 9, 2014 { 3 Comments } By Sivateja

By using @FormParam annotation, RESTful web service would accept HTML form parameters sent by the client in the POST request and bind them to the method variables. Generally @FormParam will come into picture when client send the data in POST request, if its the GET request @QueryParam would be the best choice.

Let me give you an example on usage of @FormParam in the JAX-RS.

Note:
If you are new to RESTful web services, first go through ‘Jersey Hello World Example Using JAX-RS Specification‘ there you can learn each and every step to create a RESTful web service in eclipse, how to install maven and configuration settings related to JAX-RS.

Required Files

  • pom.xml and web.xml are similar to the previous article
  • RestServiceFormParamJava4s.java
  • Client.html

RestServiceFormParamJava4s.java

package com.java4s;
 
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
 
@Path("/customers")
public class RestServiceFormParamJava4s {
    
    @POST
    @Path("/addCustomer")
    @Produces("text/html")
    public Response getResultByPassingValue(
                    @FormParam("nameKey") String name,
                    @FormParam("countryKey") String country) {
        
        String output = "<font face='verdana' size='2'>" +
                "Web Service has added your Customer information with Name - <u>"+name+"</u>, Country - <u>"+country+"</u></font>";
        return Response.status(200).entity(output).build();
 
    }
}

 Client.html

<html>
<head>
    <title>RESTful Web Services (JAX-RS) @FormParam Exampale</title>
</head>
<body>
    
    <form action="http://localhost:2013/RestFormParamAnnotationExample/rest/customers/addCustomer" method="post">

        <table>
            <tr>
                <td><font face="verdana" size="2px">Customer Name : </font></td>
                <td><input type="text" name="nameKey" /> </td>
            </tr>

            <tr>
                <td><font face="verdana" size="2px">Country</font></td>
                <td> <input type="text" name="countryKey" /> </td>
            </tr>

            <tr>
                <td></td>
                <td><input type="submit" value="Add Customer" /> </td>
            </tr>
        </table>            

    </form>
 
</body>
</html>

Explanation

  • Right click on your project root folder > Run As > Run on Server
  • Eclipse will open http://localhost:2013/<projectRootFolder> with 404 Error by default, forget about that
  • Now open Client.html in your web browser, enter the details and click submit [ I have created this .html file to send input form parameters to our RESTful service, you no need to create & place this file in the project workspace, myself i have created client.html file in my desktop and open in Google chrome, and verified the output]
  • In Client.html, observe the URL in the from action [ line number 7 ]
  • Once you click on Submit, Client.html will POST the data to the restful service. From there REST service will retrieve those details by using @FormParam annotation.

RememberInput field names in Client.html [ line numbers 12,17 ] should match with @FormParam(“-“) parameters[ line numbers 16,17 ] in RestServiceFormParamJava4s.java

Output

Input:

Output:

 

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

3 Responses to “RESTful Web Services (JAX-RS) @FormParam Example”
  1. Rahul says:

    How to create client for this example

  2. bindu says:

    Hi i am a learner of web services.Firstly very well explained tutorial.I am building service with a pojo object having two fields user,email.I have same field in my client form which i am submitting.Here i am using @RequestBody so that it will map the fields on the fly.
    But i am facing an issue like ,
    WARNING: No message body reader has been found for request class Employee, ContentType : application/x-www-form-urlencoded.
    Jan 27, 2016 10:13:10 PM org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse

    I didnt understand exactly where i am missing.Could you please help

  3. vanaja says:

    Hi,
    as explained ealier "We will use @Produces annotation for GET requests only".But in above example I can see @produces for POST request.Can you explain me why you used @produces annotation for POST request.

    Thanks,
    Vanaja.G

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.