Javatpoint Logo
Javatpoint Logo

Enhancing the Hello World Service with a Path Variable

The @PathVariable annotation is used to extract the value from the URI. It is most suitable for the RESTful web service where the URL contains some value. Spring MVC allows us to use multiple @PathVariable annotations in the same method. A path variable is a critical part of creating rest resources.

We will create another hello-world-bean request with a path parameter.

Step 1: Open the HelloWorldController.java file and add another helloWorldBean() service.

HelloWorldController.java

Whatever value we will pass to the path variable is picked up by the controller and returned to the response.

Step 2: Type the URL http://localhost:8080///hello-world/path-variable/javatpoint

Step 3: Run the HelloWorldController.java file. We get the following response on the browser.

Enhancing the Hello World Service with a Path Variable

Let's change the path variable once again: http://localhost:8080/hello-world/path-variable/Anubhav

Enhancing the Hello World Service with a Path Variable

We can see that whatever we are writing in the path variable is returned to the response.

Creating User Bean and User Service

In this section, we are going to create real resources users and the post. We will use a static array list to represent the data.

Step 1: Create a new package with the name com.javatpoint.server.main.user.

Step 2: Create a bean class (User) to store the user detail.

Right click on the package user -> New -> Class -> Provide Name -> Finish. Here, we have provided class name User.

Step 3: Define three private variables id, name, and dob.

Step 4: Generate Getters and Setters.

Right click on the file -> Source -> Generate Getters and Setters... ->Select All -> Generate.

Step 5: Generate toString.

Right click on the file -> Source -> Generate toString... ->Select All -> Generate.

Step 6: Generate Constructors.

Right click on the file -> Source -> Generate Constructor using Fields... -> Generate.

User.java

Before moving to the next step first move HelloWorldBean.java and HelloWorldController.java in the package com.javatpoint.server.main.helloworld.

Step 7: Create a class with name UserDaoService in the package com.javatpoint.server.main.user.

UserDaoService.java

Implementing Get Methods for User Resource

Step 8: Now create a user controller class with name UserResource.

UserResource.java

Step 9: Run the application and type the localhost:8080/users in the address bar of the browser. It returns the users list in JSON format.

Enhancing the Hello World Service with a Path Variable

If the date is displaying in the default timestamp format as:

"dob": "1500370250075"

We need to do setting for proper date format.

Open the application.properties file. Remove the debug configuration and add the following configuration:

The above statement telling the Jackson framework that when serializing don't treat the date as a timestamp.

Step 10: If we want to display a specific user detail on the browser, add the mapping "/user/{id}" and create a method retriveUser() in the UserResource.

UserResource.java

Step 11: Run the application and type localhost:8080/users/{id} in the browser. It returns the detail of the specific user id which we are passing in the path variable.

In the following image, we have retrieved the detail of the user having id 4.

Enhancing the Hello World Service with a Path Variable








Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA