Path, PathParam, and ApplicationPath

Start course
Overview
Difficulty
Intermediate
Duration
1h 8m
Students
3
Description

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.

Prerequisites

Basic knowledge of Java programming.

Transcript

Hello dear friends. In this video, we will start to examine JAX-RS annotations with Path, PathParam and ApplicationPath. Let's begin. Before we start on annotations, we have to learn the resource route class. Route resource classes are Plain Old Java Objects as POJO that are either annotated with Path or have at least one method annotated with Path or a request method designator such as GET, PUT, POST or DELETE. Resource methods are methods of a resource class annotated with a request method designator. I think that's enough. Let's now move on to annotations. We will start with Path. The Path annotation identifies the URI path template to which the resource responds, and is specified at the class or method level of a resource. The Path annotation's value is a partial URI path template relative to the base URI of the server on which the resource is deployed.

The context route of the application and the URL pattern to which the JAX-RS runtime responds. Let's look at this image. In Path, as you can see, we have an employee text. This means we have to access this service with route link and employee. If we change this with employee first service, we have to add first service to our URI, like this. We can also use Path annotation with methods, for example, in this method, we have another Path. This means we have access to this method with localhost.../employee/getName. The path used is this. URI path templates are URIs with variables embedded within the URI syntax. These variables are substituted at runtime. In order for a resource to respond to a request based on the substituted URI, variables are denoted by URLE braces. Before I provide you with an example, we have to learn PathParam annotation. PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call.

For example, let's look at this code snip. There's a PathParam annotation with name variable. If we give a name like Charles, this means we have to use localhost.../employee/Charles URL. In this kind of example, a user is prompted to type his or her name and then a JAX-RS web service configured to respond to requests to this URI path template responds. For example, if the user types the username Charles, the web service responds to the following URL. Localhost.../employee/Charles. Let's look at another example. In this example, as you can see, there are two variables and two PathParam. For example, Charles and 15 parameters, this service responds localhost.../employee/Charles/15 URL. Or let's look at another. For John and Verdon parameters, this service responds localhost.../Author/john-Verdon. I hope you now have a better understanding of Path and PathParam.

There is another annotation related to Path and that is the ApplicationPath. ApplicationPath annotation identifies the application path that serves as the base URI for all resources. This should be used on the configuration class i.e. the subclass of the Java X WS-RS core application. For example, if we use ApplicationPath for application class and we will code in API class like this. Now this method responds localhost.../api/employee/getName URL. Actually, ApplicationPath is not much preferred. However, it will be useful for you to learn this information. In the next video, we will examine Request Method Designators. See you in the next video.

 

About the Author
Students
1180
Courses
63
Learning Paths
4

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