Using
@RequestParam annotation, you can map request parameters to controller method
parameters.
Example
@RequestMapping("/") public String home(@RequestParam(name = "name", defaultValue = "user") String userName, @RequestParam(name = "gender", defaultValue = "m") String gender) { ...... ...... }
For the
uri http://localhost:8080/?name=sailu&gender=f, userName mapped to sailu
and gender mapped to f.
Find the
below working application.
package com.sample.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
HomeController.java
package com.sample.app.cotroller; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HomeController { @RequestMapping("/") public String home(@RequestParam(name = "name", defaultValue = "user") String userName, @RequestParam(name = "gender", defaultValue = "m") String gender) { StringBuilder builder = new StringBuilder(); builder.append("Hello, "); if("f".equalsIgnoreCase(gender)) { builder.append(" Madam "); }else { builder.append("Mr. "); } builder.append(userName).append(". Have a great day!!!!!!"); return builder.toString(); } @RequestMapping("/headers") public String headers(HttpServletRequest request) { Enumeration<String> headerNames = request.getHeaderNames(); StringBuilder builder = new StringBuilder(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); String headerValue = request.getHeader(headerName); builder.append(headerName).append(" : ").append(headerValue).append("<br />"); } return builder.toString(); } @RequestMapping("/userAgent") public ResponseEntity<String> getData(@RequestHeader(value = "user-agent") String userAgent) { return ResponseEntity.ok(userAgent); } }
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>springRest</groupId> <artifactId>springRest</artifactId> <version>1</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> </parent> <name>springbootApp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Total
project structure looks like below.
Run
App.java.
Open the
url 'http://localhost:8080/?name=sailu&gender=f' in browser, you can see
below welcome screen.
You can
download complete working application from this link.
No comments:
Post a Comment