Get all updates of Java4s
Newsletter

RESTful Web Services (JAX-RS) @FormParam Example

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

 Client.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.

Remember:¬† Input 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

Comments

2 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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Spring-Boot-tutorials Hibernate tutorials Spring tutorials Struts tutorials Servlets tutorials Ajax tutorials Log4j tutorials jQuery tutorials Json tutorials Core Java tutorials Web Services tutorials AngularJs tutorials
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Contact | About Us | Privacy Policy | Advertise With Us

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