@PathVariable and @RequestParam annotations in Spring Boot

In this post we will see @PathVariable and @RequestParam annotations in Spring Boot.

Basic points about @PathVariable and @RequestParam annotations.

@PathVariable annotation-

  • This annotation introduced in Spring  3.0, available in org.springframework.web.bind.annotation package.
  • Optional elements  ( name, required, value).
  • This annotation used as a method parameter.
  • It takes placeholder value from URI.
  • The example of rest URI when we use @PathVariable – http://localhost:9093/rest/listofbooks/{bookId}

@RequestaParam annotation-

  • This annotation introduced in Spring  2.5, available in org.springframework.web.bind.annotation package.
  • Optional elements  (name, required, value).
  • This annotation used as a method parameter.
  • It takes parameter value from URI.
  • The example of rest URI when we use @RequestParam –

– http://localhost:9093/rest/listofbooks/{bookId}/book?bookName=book1

 

@PathVariable annotations Example in Spring Boot.

prerequisites –

  • JDK 1.8
  • Eclipse
  • maven
  • postman

 

Create maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – pathvariableexample, ArtifactId – pathvariableexample and name – pathvariableexample) and click on finish. Keep packaging as the jar.

Modify the pom.xml

 

Define classes.

Book.java

 

BookController.java

 

SpringMain.java

 

If you encounter any port related issue, define application.properties and change the port.

application.properties

server.port = 9093

Run the SpringMain class and deploy the server.

Test the rest URI.

@PathVariable and @RequestParam annotations in Spring Boot

@RequestParam example in Spring Boot.

Let’s modify the controller class.

BookController.java

 

@PathVariable and @RequestParam annotations in Spring Boot

That’s all about @PathVariable and @RequestParam annotations in Spring Boot. You may like.

@PathVariable docs.

Top