Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added branch for @RequestMapping #39

Open
wants to merge 1 commit into
base: spring-request-mapping
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 14 additions & 41 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,65 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
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>guru.springframework</groupId>
<artifactId>blogposts</artifactId>
<artifactId>spring-boot-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Blog Posts</name>
<description>Misc Blog Posts</description>

<name>Spring Boot Web Application</name>
<description>Spring Boot Web Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>guru.springframework.blog.BlogPostsApplication</start-class>
<java.version>1.8</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.10.Final</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
Expand All @@ -70,5 +44,4 @@
</plugin>
</plugins>
</build>

</project>
</project>

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package guru.springframework.blog.controllers;

import org.springframework.web.bind.annotation.*;
import org.springframework.http.*;

@RestController
@RequestMapping("/home")
public class IndexController {

/*@RequestMapping at method level*/
@RequestMapping("/index")
String index(){
return "Hello from index";
}
@RequestMapping(value={"", "/page", "page*","view/*"})
String indexMultipleMapping(){
return "Hello from index multiple mapping.";
}

/*@RequestMapping using HTTP request methods*/
@RequestMapping(method = RequestMethod.GET)
String get(){
return "Hello from get";
}
@RequestMapping(method = RequestMethod.DELETE)
String delete(){
return "Hello from delete";
}
@RequestMapping(method = RequestMethod.POST)
String post(){
return "Hello from post";
}
@RequestMapping(method = RequestMethod.PUT)
String put(){
return "Hello from put";
}
@RequestMapping(method = RequestMethod.PATCH)
String patch(){
return "Hello from patch";
}
/*@RequestMapping with headers*/
@RequestMapping(value = "/head", headers = {"content-type=text/plain", "content-type=text/html"})
String getWithHeaders(){
return "Mapping applied along with headers";
}

/*@RequestMapping with @RequestParam*/
@RequestMapping(value = "/personId")
String getId(@RequestParam String personId){
System.out.println("ID is "+personId);
return "Get ID from query string of URL without value element";
}
@RequestMapping(value = "/id")
String getIdByValue(@RequestParam("id") String personId){
System.out.println("ID is "+personId);
return "Get ID from query string of URL with value element";
}

@RequestMapping(value = "/name")
String getName(@RequestParam(value = "person", required = false) String personName ){
return "Required element of request param";
}

/*@RequestMapping with produces and consumes attributes*/
@RequestMapping(value = "/prod", produces = {"text/html", "application/JSON"})
@ResponseBody
String getProduces(){
return "Produces attribute";
}

@RequestMapping(value = "/cons", consumes = {"text/plain", "application/XML"})
String getConsumes(){
return "Consumes attribute";
}

/*@RequestMapping with @PathVariable for Dynamic URI */
@RequestMapping(value = "/fetch/{id}", method = RequestMethod.GET)
String getDynamicUriValue(@PathVariable String id){
System.out.println("ID is "+id);
return "Dynamic URI parameter fetched";
}
@RequestMapping(value = "/fetch/{id:[a-z]+}/{name}", method = RequestMethod.GET)
String getDynamicUriValueRegex(@PathVariable("name") String name){
System.out.println("Name is "+name);
return "Dynamic URI parameter fetched using regex";
}

/*@RequestMapping with params attribute*/
@RequestMapping(value = "/fetch", params = {"personId=10"})
String getParams(@RequestParam("personId") String id){
return "Fetched parameter using params attribute = "+id;
}

@RequestMapping(value = "/fetch", params = {"personId=20"})
String getParamsDifferent(@RequestParam("personId") String id){
return "Fetched parameter using params attribute = "+id;
}

/*@RequestMapping for default method*/
@RequestMapping()
String defaultMethod(){
return "This is a default method for the class";
}

/*@RequestMapping shortcuts*/
@GetMapping("/person")
public @ResponseBody ResponseEntity<String> getPerson() {
return new ResponseEntity<String>("Response from GET", HttpStatus.OK);
}

@GetMapping("/person/{id}")
public @ResponseBody ResponseEntity<String> getPersonById(@PathVariable String id){
return new ResponseEntity<String>("Response from GET with id " + id, HttpStatus.OK);
}

@PostMapping("/person/post")
public @ResponseBody ResponseEntity<String> postPerson() {
return new ResponseEntity<String>("Response from POST method", HttpStatus.OK);
}

@PutMapping("/person/put")
public @ResponseBody ResponseEntity<String> putPerson() {
return new ResponseEntity<String>("Response from PUT method", HttpStatus.OK);
}

@DeleteMapping("/person/delete")
public @ResponseBody ResponseEntity<String> deletePerson() {
return new ResponseEntity<String>("Response from DELETE method", HttpStatus.OK);
}

@PatchMapping("/person/patch")
public @ResponseBody ResponseEntity<String> patchPerson() {
return new ResponseEntity<String>("Response from PATCH method", HttpStatus.OK);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading