Extracting Request Parameters

Start course
1h 8m

This course takes an in-depth look at Java API for RESTful Web Services, otherwise known as JAX-RS. We also run through some example exam questions that will be useful for anyone who wants to take Oracle's Java EE exam.

Learning Objectives

  • Understand the fundamentals of JAX-RS
  • Learn about the main concepts and components of JAX-RS

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.


Basic knowledge of Java programming.


Hello dear friends. In this video, we will examine the Extracting Request Parameters, let's begin. RESTful Web Services exchange representations using URIs. However, because we use Web Technologies, and because of design decisions, we don't always use representation such as XML Structures. We sometimes need to provide other means to transfer data, such as plain HTML Forms. This means that if a Client sends a request from an HTML Form, we need to get access to the value pair set that is part of the request. Even though we don't use the following annotation in our Web Service, you may want to take a look at it, and then decide if it makes sense to use in your solutions. There are 6 Parameter types: PathParam, QueryParam, FormParam, CookieParam, HeaderParam, and MatrixParam. I will explain them one by one. In path video, we have already learned and used PathParam. So, I will start with QueryParam.

The QueryParam Annotation allows you to inject individual URI Query Parameters into your Java Parameters. QueryParam can be used in case of filtering when you want to show both the Param and the value in the URL. For example, let's say you are showing list of customers on a website, and you can show 10 at a time. When the user clicks next, he will see the next 10 customers. So, in this case, the QueryParam URL will look like this. Look, StartParameter=10, and Size=10. The code will be like this. Here, we use the QueryParam annotation to inject the URL Query Parameters, start and size into the Java Parameters start and size. As with other Annotation Injection, JAX-RS automatically converts the Query Parameter string into an integer. You may have the need to iterate through all Query Parameters to find on the Request URI. The javax.ws.rs.code.uri info interface has a getQueryParameters method that gives you a map containing all QueryParameters.

You can inject instances of URI info using the javax.ws.rs.code.ContextAnnotation. There is another example of injecting this class and using it to obtain the value of a few Query Parameters. Look, with getQueryParameter method, we can take Start and Size Parameters. Now, that's enough about QueryParam. Let's go on with FormParam. The FormParam Annotation is used to access Application Form URLEncoded Request Bodies. In other words, it's used to access individual entries posted by an HTML Form document. For example, let's say we set up a form on our website to register new customers. You may want to take Name, Surname, and Email. In HTML Page, you will set input name values like this. In the Service Page, you will use FormParam with all needed parameters like this. Here, we are injecting Name, Surname, and Email from the HTML Form into the Java Parameters, Name, Surname, and Email. Form data is URLencoded when it goes across the wire. When using FormParam, JAX-RS will automatically decode the form entries value before injecting it.

The other one is CookieParam. Servers can store state information in cookies on the Client, and can retrieve that information when the Client makes its next request. Many Web Applications use Cookies to set up a session between the Client and the Server. They also use Cookies to remember identity and user preferences between requests. These Cookie values are transmitted back and forth between the Client and the Server via Cookie Headers. The CookieParam Annotation allows you to inject cookies sent by a Client request into your JAX-RS Resource methods. For example, let's say our application push an Employee ID Cookie to our Clients so that we can track users as they invoke and interact with our Web Services. The code will be like this. The use of CookieParam here, makes the JAX-RS provider search all Cookie Headers for the Employee ID Cookie value. It then converts it into an int and injects it into the Cost ID Parameter.

If you need more information about the cookie other than its base value, you can instead inject a JavaX WS-RS Core Cookie Object. Let's examine the Cookie Object. The Cookie class has an additional contextual information about the Cookie beyond its name and value. Like, getName, getValue, getVersion, getDomain, and getPath. Now, let's go on with HeaderParam. The HeaderParam Annotation is used to inject HTTP Request Header values. For example, what if your application was interested in the Web Page that referred to or linked to your Web Service? You could use the HTTP Referer Header using the HeaderParam Annotation. The HeaderParam Annotation is pulling the Referer Header directly from the HTTP request and injecting it into the Referral Method Parameter. But sometimes you need programmatic access to view all headers within the incoming request.

For instance, you may want to log them. The JAX-RS specification provides the HTTP Headers Interface for such scenarios. The getRequestHeader method allows you to get access to one particular header. And getRequestHeaders gives you a map that represents all headers. As with URI info, you can use the Context Annotation to obtain an instance of HTTP Headers. As you can see in this image, we can also use Context Annotation. Now, the last one is MatrixParam. Instead of injecting and processing path segment objects to obtain matrix parameter values, the JAX-RS Specification allows you to inject Matrix Parameter values directly through the Javax WS-RS MatrixParam Annotation. The Annotation, MatrixParam, is used to capture Matrix Parameters. We would use this Annotation as alternative to QueryParam. Matrix Parameters are more powerful than Query Parameters because they can be inserted in the middle of the URL. Let's examine the usage of MatrixParam by code snips. Let's examine this code. If we call this service with person 2022, the name and age will be null. If we call with person 2022 Charles, the result will be Year 2022 and name Charles. If we call person 2022 name Charles, and Age 25, you can see all needed values. Now, we have finished Extracting Request Parameters. In the next video, we will start to solve topic questions. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics