@PathVariable and @RequestParam annotations in Spring Boot Part 2
Tags: springboot, interviewquestions, java, annotations
Published: 2020-11-16 17:51:38

<hr />

<strong>Table of Contents:</strong>
<ol>
 	<li><a href="#PathVariable_and_@RequestParam_annotations">Basic points about @PathVariable and @RequestParam annotations.</a></li>
 	<li><a href="#PathVariable_annotations">@PathVariable annotations Example in Spring Boot.</a></li>
 	<li><a href="#RequestParam_example">@RequestParam example in Spring Boot.</a></li>
</ol>
<h2 id="PathVariable_and_@RequestParam_annotations">Basic points about @PathVariable and @RequestParam annotations.</h2>
<strong>@PathVariable annotation-</strong>
<ul>
 	<li>This annotation introduced in Spring  3.0, available in <strong>org.springframework.web.bind.annotation package</strong>.</li>
 	<li>Optional elements  ( name, required, value).</li>
 	<li>This annotation used as a method parameter.</li>
 	<li>It takes placeholder value from <strong>URI</strong>.</li>
 	<li>The example of rest <strong>URI</strong> when we use <strong>@PathVariable</strong> – <strong>http://localhost:9093/rest/listofbooks/{bookId}</strong></li>
</ul>
<strong>@RequestaParam annotation-</strong>
<ul>
 	<li>This annotation introduced in Spring  2.5, available in org.springframework.web.bind.annotation package.</li>
 	<li>Optional elements  (name, required, value).</li>
 	<li>This annotation used as a method parameter.</li>
 	<li>It takes parameter value from URI.</li>
 	<li>The example of rest URI when we use @RequestParam –</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">– http://localhost:9093/rest/listofbooks/{bookId}/book?bookName=book1</pre>
<h2 id="PathVariable_annotations">@PathVariable annotations Example in Spring Boot.</h2>
prerequisites –
<ul>
 	<li>JDK 1.8</li>
 	<li>Eclipse</li>
 	<li>maven</li>
 	<li>postman</li>
</ul>
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.

<strong>POM.XML</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">&lt;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"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;pathvariableexample&lt;/groupId&gt;
  &lt;artifactId&gt;pathvariableexample&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;pathvariableexample&lt;/name&gt;  
  &lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;1.5.2.RELEASE&lt;/version&gt;
 &lt;/parent&gt;
  &lt;dependencies&gt;
    &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;
   &lt;/dependencies&gt;
 &lt;/project&gt;</pre>
<strong>Book.java</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">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;
    }
    
}</pre>
<strong>BookController.java</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">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&lt;Book&gt; listBook = createBook();
        for(Book book : listBook) {
            if(book.getBookId() == 1) {
                return book;
            }
        }
    	
    	return null;		
    }
    
    public List&lt;Book&gt; 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&lt;Book&gt; bookList = new ArrayList&lt;Book&gt;();
    	bookList.add(book);
    	bookList.add(book1);
    	
    	return bookList;
    	
    }
}</pre>
<strong>SpringMain.java</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">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);
    }
}</pre>
<strong>Now hit the URL:</strong>

<a href="http://localhost:8080:/rest/book/1">http://localhost:8080:/rest/book/1</a>
<h2 id="RequestParam_example"><strong>@RequestParam example in Spring Boot.</strong></h2>
Let’s modify the controller class.

<strong>BookController.java</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">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&lt;Book&gt; listBook = createBook();
        for(Book book : listBook) {
            System.out.println("bookIs is ---"+bookId);
            System.out.println("bookName is ---"+bookName);
            if(book.getBookId() == 1 &amp;&amp; book.getBookName().equals(bookName)) {
                return book;
            }
        }
    	return null;
    }
    
    public List&lt;Book&gt; 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&lt;Book&gt; bookList = new ArrayList&lt;Book&gt;();
    	bookList.add(book);
    	bookList.add(book1);
    	
    	return bookList;
    	
    }
}</pre>
Now hit the URL to see the OutPut:

<a href="http://localhost:8080:/rest/listpfbooks/1/book?bookName=book1"><strong>http://localhost:8080:/rest/listpfbooks/1/book?bookName=book1</strong></a>

Source: <a href="https://howtostartprogramming.in/category/java/" rel="dofollow">Java Algos</a>
<hr />
<hr />