@PathVariable and @RequestParam annotations in Spring Boot Part 2

rajesh1761

Rajesh Mishra

Posted on November 16, 2020

@PathVariable and @RequestParam annotations in Spring Boot Part 2

Table of Contents:

  1. Basic points about @PathVariable and @RequestParam annotations.
  2. @PathVariable annotations Example in Spring Boot.
  3. @RequestParam example 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.

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>pathvariableexample</groupId>
  <artifactId>pathvariableexample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>pathvariableexample</name>  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
 </parent>
  <dependencies>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
   </dependencies>
 </project>

Book.java

package com.onlintutorials.tech;

public class Book {
    int bookId;
    String bookName;
    String bookPrice;
    public int getBookId() {
        return bookId;
    }
    public void setBookId(int bookId) {
        this.bookId = bookId;
    }
    public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public String getBookPrice() {
        return bookPrice;
    }
    public void setBookPrice(String bookPrice) {
        this.bookPrice = bookPrice;
    }
    
}

BookController.java

package com.onlinetutotrials.tech;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

@RestController
@RequestMapping(value = "/rest")
public class BookController {

    @RequestMapping(value = "/book/{bookId}",method = RequestMethod.GET)
    public Book getBookById(@PathVariable int bookId) {
        
        List<Book> listBook = createBook();
        for(Book book : listBook) {
            if(book.getBookId() == 1) {
                return book;
            }
        }
        
        return null;        
    }
    
    public List<Book> createBook() {
    Book book = new Book();
        book.setBookId(1);
        book.setBookName("book1");
        book.setBookPrice("100");
        
        Book book1 = new Book();
        book1.setBookId(2);
        book1.setBookName("book2");
        book1.setBookPrice("200");
        List<Book> bookList = new ArrayList<Book>();
        bookList.add(book);
        bookList.add(book1);
        
        return bookList;
        
    }
}

SpringMain.java

package com.onlinetutorials.tech;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringMain {
    public static void main(final String[] args) {
        final ConfigurableApplicationContext configurableApplicationContext = SpringApplication
                .run(SpringMain.class, args);
    }
}

Now hit the URL:

http://localhost:8080:/rest/book/1

@RequestParam example in Spring Boot.

Let’s modify the controller class.

BookController.java

package com.onlinetutorials.tech;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

@RestController
@RequestMapping(value = "/rest")
public class BookController {

    @RequestMapping(value = "/listofbooks/{bookId}/book",method = RequestMethod.GET)
    public Book getBookById(
            @PathVariable int bookId, 
            @RequestParam String bookName) {
        
        List<Book> listBook = createBook();
        for(Book book : listBook) {
            System.out.println("bookIs is ---"+bookId);
            System.out.println("bookName is ---"+bookName);
            if(book.getBookId() == 1 && book.getBookName().equals(bookName)) {
                return book;
            }
        }
        return null;
    }
    
    public List<Book> createBook() {
        
        Book book = new Book();
        book.setBookId(1);
        book.setBookName("book1");
        book.setBookPrice("100");
        
        Book book1 = new Book();
        book1.setBookId(2);
        book1.setBookName("book2");
        book1.setBookPrice("200");
        List<Book> bookList = new ArrayList<Book>();
        bookList.add(book);
        bookList.add(book1);
        
        return bookList;
        
    }
}

Now hit the URL to see the OutPut:

http://localhost:8080:/rest/listpfbooks/1/book?bookName=book1

Source: Java Algos





💖 💪 🙅 🚩
rajesh1761
Rajesh Mishra

Posted on November 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related