Skip to content

Commit 9c3ecc4

Browse files
committed
Using New Composed Annotations for @RequestMapping in Spring Framework
1 parent 5d0fd95 commit 9c3ecc4

File tree

14 files changed

+364
-0
lines changed

14 files changed

+364
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="output" path="target/classes"/>
26+
</classpath>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>spring-new-reqmapping-annotations</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding/<project>=UTF-8
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Using New Composed Annotations for @RequestMapping in Spring Framework
2+
3+
4+
## 1. Import source code into Eclipse
5+
6+
Menu **File –> Import –> Maven –> Existing Maven Projects**
7+
8+
Browse to your source code location
9+
10+
Click **Finish** button to finish the importing
11+
12+
## 2. Run the example
13+
14+
Open the **Application.java**
15+
16+
**Right click -> Run As -> Java Application** or use the shortcut: **Alt+Shift+x, j** to start the main method
17+
18+
19+
###[Using New Composed Annotations for @RequestMapping in Spring Framework](http://howtoprogram.xyz/2017/03/16/using-new-composed-annotations-of-requestmapping-in-spring-framework/)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.howtoprogram</groupId>
7+
<artifactId>spring-new-reqmapping-annotations</artifactId>
8+
<version>0.1.0</version>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.5.2.RELEASE</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-data-jpa</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.h2database</groupId>
27+
<artifactId>h2</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-test</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
35+
</dependencies>
36+
37+
<properties>
38+
<java.version>1.8</java.version>
39+
</properties>
40+
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-maven-plugin</artifactId>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
51+
<repositories>
52+
<repository>
53+
<id>spring-releases</id>
54+
<url>https://repo.spring.io/libs-release</url>
55+
</repository>
56+
</repositories>
57+
<pluginRepositories>
58+
<pluginRepository>
59+
<id>spring-releases</id>
60+
<url>https://repo.spring.io/libs-release</url>
61+
</pluginRepository>
62+
</pluginRepositories>
63+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
import org.springframework.boot.SpringApplication;
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.context.annotation.ComponentScan;
5+
6+
@SpringBootApplication
7+
@ComponentScan({"com.howtoprogram.book", "com.howtoprogram.service"})
8+
public class Application {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(Application.class, args);
12+
}
13+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.howtoprogram.book;
2+
3+
import javax.xml.bind.annotation.XmlRootElement;
4+
5+
@XmlRootElement(name = "book")
6+
public class Book {
7+
8+
private Long id;
9+
private String name;
10+
private String author;
11+
12+
public Book() {
13+
14+
}
15+
// getter
16+
17+
public Book(Long id, String name, String author) {
18+
super();
19+
this.id = id;
20+
this.name = name;
21+
this.author = author;
22+
}
23+
24+
/**
25+
* @return the id
26+
*/
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
/**
32+
* @param id the id to set
33+
*/
34+
public void setId(Long id) {
35+
this.id = id;
36+
}
37+
38+
/**
39+
* @return the name
40+
*/
41+
public String getName() {
42+
return name;
43+
}
44+
45+
/**
46+
* @param name the name to set
47+
*/
48+
public void setName(String name) {
49+
this.name = name;
50+
}
51+
52+
/**
53+
* @return the author
54+
*/
55+
public String getAuthor() {
56+
return author;
57+
}
58+
59+
/**
60+
* @param author the author to set
61+
*/
62+
public void setAuthor(String author) {
63+
this.author = author;
64+
}
65+
66+
/*
67+
* (non-Javadoc)
68+
*
69+
* @see java.lang.Object#toString()
70+
*/
71+
@Override
72+
public String toString() {
73+
return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
74+
}
75+
76+
77+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.howtoprogram.book;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.DeleteMapping;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PatchMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.PutMapping;
12+
import org.springframework.web.bind.annotation.RequestBody;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
@RestController
17+
@RequestMapping(value = "/v1")
18+
public class BookController {
19+
20+
@Autowired
21+
private BookRepository bookRepository;
22+
23+
@GetMapping(value = "/books")
24+
public ResponseEntity<Iterable<Book>> getAllBooks() {
25+
return new ResponseEntity<>(bookRepository.findAll(), HttpStatus.OK);
26+
}
27+
28+
@PostMapping(value = "/books")
29+
public ResponseEntity<Book> createBook(@RequestBody Book book) {
30+
bookRepository.save(book);
31+
return new ResponseEntity<>(book, HttpStatus.CREATED);
32+
}
33+
34+
@PutMapping(value = "/books/{id}")
35+
public ResponseEntity<Book> readBook(@PathVariable("id") Long id) {
36+
Book book = bookRepository.findOne(id);
37+
if (book != null) {
38+
return new ResponseEntity<>(book, HttpStatus.OK);
39+
} else {
40+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
41+
}
42+
}
43+
44+
@DeleteMapping(value = "/books/{id}")
45+
public ResponseEntity<Book> deleteBook(@PathVariable("id") Long id) {
46+
Book book = bookRepository.findOne(id);
47+
if (book != null) {
48+
return new ResponseEntity<Book>(HttpStatus.NOT_FOUND);
49+
}
50+
51+
return new ResponseEntity<Book>(HttpStatus.NO_CONTENT);
52+
}
53+
54+
@PutMapping(value = "/books/{id}")
55+
public ResponseEntity<Book> updateBook(@PathVariable("id") Long id,
56+
@RequestBody Book Book) {
57+
Book dbBook = bookRepository.findOne(id);
58+
if (dbBook == null) {
59+
return new ResponseEntity<Book>(HttpStatus.NOT_FOUND);
60+
}
61+
dbBook.setName(Book.getName());
62+
dbBook.setAuthor(Book.getAuthor());
63+
return new ResponseEntity<Book>(dbBook, HttpStatus.OK);
64+
}
65+
66+
@PatchMapping(value = "/books/{id}")
67+
public ResponseEntity<Book> updatePartially(@PathVariable("id") Long id,
68+
@RequestBody Book Book) {
69+
Book dbBook = bookRepository.findOne(id);
70+
if (dbBook == null) {
71+
return new ResponseEntity<Book>(HttpStatus.NOT_FOUND);
72+
}
73+
dbBook.setName(Book.getName());
74+
dbBook.setAuthor(Book.getAuthor());
75+
return new ResponseEntity<Book>(dbBook, HttpStatus.OK);
76+
}
77+
}

0 commit comments

Comments
 (0)