☰ See All Chapters |
REST Parameters
In rest we can provide multiple ways to service consumer to pass the input. There are three ways to pass the input in service URI and one way to through forms. JAX-RS provides the below annotation to receive the input sent by the consumer/client.
@PathParam
@QueryParam
@MatrixParam
@FormParam
PathParam, QueryParam, MatrixParam allows to send the data in URI as a part of URL. FormParm allows to send the data in html/JSP form.
PathParam URL Syntax
Each parameter should be separated by / (forward slash)
package com.java4coding;
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType;
@Path("/hello") public class RestServicePathParam { @GET @Path("{firstName}/{lastName}") @Produces(MediaType.TEXT_PLAIN) public String sayHello(@PathParam("firstName") String firstName, @PathParam("lastName") String lastName) { return "Hello " + firstName+ " "+ lastName; } } |
URL --> https://localhost:8090/JAX_RS_PathParamAnnotationExample/rest/hello/Manu/Manjunatha
QueryParam URL Syntax
Parameters should be sent as query string in URL. Query string should start with ? (Question Mark), parameter and value should be separated by = (equals), each parameter value pair should be separated by & (Ampersand)
package com.java4coding;
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType;
@Path("/hello") public class RestServiceQueryParam { @GET @Produces(MediaType.TEXT_PLAIN) public String sayHello(@QueryParam("firstName") String firstName, @QueryParam("lastName") String lastName) { return "Hello " + firstName+ " "+ lastName; } } |
URL --> https://localhost:8090/JAX_RS_QueryParamAnnotationExample/rest/hello?firstName=Manu&lastName=Manjunatha
MatrixParam URL Syntax
Parameter and value should be separated by = (equals), each parameter value pair should be separated by ; (Semicolon)
package com.java4coding;
import javax.ws.rs.GET; import javax.ws.rs.MatrixParam; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType;
@Path("/hello") public class RestServiceMatrixParam { @GET @Produces(MediaType.TEXT_PLAIN) public String sayHello(@MatrixParam("firstName") String firstName, @MatrixParam("lastName") String lastName) { return "Hello " + firstName+ " "+ lastName; } } |
URL --> https://localhost:8090/JAX_RS_MatrixParamAnnotationExample/rest/hello;firstName=Manu;lastName=Manjunatha
FormParam URL Syntax
Values can be sent in HTML/JSP form fields.
package com.java4coding;
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.MediaType;
@Path("/hello") public class RestServiceFormParam { @POST @Produces(MediaType.TEXT_PLAIN) public String sayHello(@FormParam("firstName") String firstName, @FormParam("lastName") String lastName) { return "Hello " + firstName+ " "+ lastName; } } |
client.html
<html> <body> <form action="https://localhost:8090/JAX_RS_FormParamAnnotationExample/rest/hello" method="post"> First Name : <input type="text" name="firstName" /><br></br> Last Name :<input type="text" name="lastName" /><br></br> <input type="submit" value="Submit" /> </form> </body> </html> |
Open client.html in web browser
Enter First Name and Last Name and click Submit.
After clicking on Submit
All Chapters