Skip to content

Commit fe841fe

Browse files
asif-anwarEugen
authored andcommitted
spring-5-mvc (eugenp#1946)
* Spring5 MVC Project * Spring5 MVC Project
1 parent 404ea81 commit fe841fe

13 files changed

Lines changed: 473 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<module>solr</module>
123123
<module>spark-java</module>
124124
<module>spring-5</module>
125+
<module>spring-5-mvc</module>
125126
<module>spring-akka</module>
126127
<module>spring-amqp</module>
127128
<module>spring-all</module>

‎spring-5-mvc/.gitignore‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
*.class
2+
3+
# Mobile Tools for Java (J2ME)
4+
.mtj.tmp/
5+
6+
.settings/
7+
.classpath
8+
.project
9+
target/
10+
11+
*.iml
12+
.idea
13+
14+
# Package Files #
15+
*.jar
16+
*.war
17+
*.ear
18+
19+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
20+
hs_err_pid*

‎spring-5-mvc/pom.xml‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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.baeldung</groupId>
7+
<artifactId>spring-5-mvc</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-5-mvc</name>
12+
<description>spring 5 MVC sample project about new features</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.0.M1</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-data-jpa</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-security</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-validation</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
</dependency>
38+
39+
40+
<!-- utils -->
41+
<dependency>
42+
<groupId>org.apache.commons</groupId>
43+
<artifactId>commons-lang3</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.slf4j</groupId>
47+
<artifactId>slf4j-api</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.slf4j</groupId>
51+
<artifactId>jcl-over-slf4j</artifactId>
52+
</dependency>
53+
54+
<!-- runtime and test scoped -->
55+
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-devtools</artifactId>
59+
<scope>runtime</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.h2database</groupId>
63+
<artifactId>h2</artifactId>
64+
<scope>runtime</scope>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.springframework</groupId>
69+
<artifactId>spring-test</artifactId>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-starter-test</artifactId>
74+
<scope>test</scope>
75+
</dependency>
76+
77+
<dependency>
78+
<groupId>junit</groupId>
79+
<artifactId>junit</artifactId>
80+
<scope>test</scope>
81+
</dependency>
82+
83+
<dependency>
84+
<groupId>com.jayway.restassured</groupId>
85+
<artifactId>rest-assured</artifactId>
86+
<version>${rest-assured.version}</version>
87+
<scope>test</scope>
88+
</dependency>
89+
90+
</dependencies>
91+
92+
<build>
93+
<plugins>
94+
<plugin>
95+
<groupId>org.apache.maven.plugins</groupId>
96+
<artifactId>maven-surefire-plugin</artifactId>
97+
<configuration>
98+
<testFailureIgnore>true</testFailureIgnore>
99+
<failIfNoTests>false</failIfNoTests>
100+
<excludes>
101+
<exclude>**/*IntegrationTest.java</exclude>
102+
<exclude>**/*LiveTest.java</exclude>
103+
</excludes>
104+
</configuration>
105+
</plugin>
106+
107+
</plugins>
108+
</build>
109+
110+
<repositories>
111+
<repository>
112+
<id>spring-snapshots</id>
113+
<name>Spring Snapshots</name>
114+
<url>https://repo.spring.io/snapshot</url>
115+
<snapshots>
116+
<enabled>true</enabled>
117+
</snapshots>
118+
</repository>
119+
<repository>
120+
<id>spring-milestones</id>
121+
<name>Spring Milestones</name>
122+
<url>https://repo.spring.io/milestone</url>
123+
<snapshots>
124+
<enabled>false</enabled>
125+
</snapshots>
126+
</repository>
127+
</repositories>
128+
<pluginRepositories>
129+
<pluginRepository>
130+
<id>spring-snapshots</id>
131+
<name>Spring Snapshots</name>
132+
<url>https://repo.spring.io/snapshot</url>
133+
<snapshots>
134+
<enabled>true</enabled>
135+
</snapshots>
136+
</pluginRepository>
137+
<pluginRepository>
138+
<id>spring-milestones</id>
139+
<name>Spring Milestones</name>
140+
<url>https://repo.spring.io/milestone</url>
141+
<snapshots>
142+
<enabled>false</enabled>
143+
</snapshots>
144+
</pluginRepository>
145+
</pluginRepositories>
146+
147+
<properties>
148+
<rest-assured.version>2.9.0</rest-assured.version>
149+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
150+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
151+
<java.version>1.8</java.version>
152+
</properties>
153+
154+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Spring5Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Spring5Application.class, args);
11+
}
12+
13+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class Foo {
10+
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.AUTO)
13+
private Long id;
14+
15+
private String name;
16+
17+
public Foo() {
18+
super();
19+
}
20+
21+
public Foo(final String name) {
22+
super();
23+
24+
this.name = name;
25+
}
26+
27+
public Foo(final long id, final String name) {
28+
super();
29+
30+
this.id = id;
31+
this.name = name;
32+
}
33+
34+
// API
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public Long getId() {
41+
return id;
42+
}
43+
44+
public void setId(Long id) {
45+
this.id = id;
46+
}
47+
48+
public void setName(final String name) {
49+
this.name = name;
50+
}
51+
52+
//
53+
54+
@Override
55+
public int hashCode() {
56+
final int prime = 31;
57+
int result = 1;
58+
result = prime * result + ((name == null) ? 0 : name.hashCode());
59+
return result;
60+
}
61+
62+
@Override
63+
public boolean equals(Object obj) {
64+
if (this == obj)
65+
return true;
66+
if (obj == null)
67+
return false;
68+
if (getClass() != obj.getClass())
69+
return false;
70+
Foo other = (Foo) obj;
71+
if (name == null) {
72+
if (other.name != null)
73+
return false;
74+
} else if (!name.equals(other.name))
75+
return false;
76+
return true;
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return "Foo [name=" + name + "]";
82+
}
83+
84+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.persistence;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
5+
import java.util.stream.IntStream;
6+
7+
import org.springframework.beans.factory.InitializingBean;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Component;
10+
11+
import com.baeldung.model.Foo;
12+
13+
@Component
14+
public class DataSetupBean implements InitializingBean {
15+
16+
@Autowired
17+
private FooRepository repo;
18+
19+
//
20+
21+
@Override
22+
public void afterPropertiesSet() throws Exception {
23+
IntStream.range(1, 5).forEach(i -> repo.save(new Foo(randomAlphabetic(8))));
24+
}
25+
26+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.persistence;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
5+
6+
import com.baeldung.model.Foo;
7+
8+
public interface FooRepository extends JpaRepository<Foo, Long>, JpaSpecificationExecutor<Foo> {
9+
10+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.web;
2+
3+
import java.util.List;
4+
5+
import javax.validation.constraints.Max;
6+
import javax.validation.constraints.Min;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.data.domain.PageRequest;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.validation.annotation.Validated;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.PutMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestParam;
18+
import org.springframework.web.bind.annotation.ResponseStatus;
19+
import org.springframework.web.bind.annotation.RestController;
20+
import org.springframework.web.server.ResponseStatusException;
21+
22+
import com.baeldung.model.Foo;
23+
import com.baeldung.persistence.FooRepository;
24+
25+
@RestController("/foos")
26+
public class FooController {
27+
28+
@Autowired
29+
private FooRepository repo;
30+
31+
// API - read
32+
33+
@GetMapping("/foos/{id}")
34+
@Validated
35+
public Foo findById(@PathVariable @Min(0) final long id) {
36+
return repo.findById(id).orElse(null);
37+
}
38+
39+
@GetMapping
40+
public List<Foo> findAll() {
41+
return repo.findAll();
42+
}
43+
44+
@GetMapping(params = { "page", "size" })
45+
@Validated
46+
public List<Foo> findPaginated(@RequestParam("page") @Min(0) final int page, @Max(100) @RequestParam("size") final int size) {
47+
return repo.findAll(PageRequest.of(page, size)).getContent();
48+
}
49+
50+
// API - write
51+
52+
@PutMapping("/foos/{id}")
53+
@ResponseStatus(HttpStatus.OK)
54+
public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) {
55+
return foo;
56+
}
57+
58+
@PostMapping
59+
@ResponseStatus(HttpStatus.CREATED)
60+
public void create( @RequestBody final Foo foo) {
61+
if (null == foo || null == foo.getName()) {
62+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST," 'name' is required");
63+
}
64+
repo.save(foo);
65+
}
66+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
server.port=8081
2+
3+
security.user.name=user
4+
security.user.password=pass
5+
6+
logging.level.root=INFO

0 commit comments

Comments
 (0)