Skip to content

Commit d8fb7d4

Browse files
committed
updates
1 parent e9fd11c commit d8fb7d4

File tree

9 files changed

+234
-0
lines changed

9 files changed

+234
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.luv2code</groupId>
6+
<artifactId>spring-rest-demo</artifactId>
7+
<version>1.0</version>
8+
<packaging>war</packaging>
9+
10+
<properties>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
</properties>
14+
15+
<dependencies>
16+
17+
<!-- Add Spring MVC and REST support -->
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-webmvc</artifactId>
21+
<version>5.0.5.RELEASE</version>
22+
</dependency>
23+
24+
<!-- Add Jackson for JSON converters -->
25+
<dependency>
26+
<groupId>com.fasterxml.jackson.core</groupId>
27+
<artifactId>jackson-databind</artifactId>
28+
<version>2.9.9.2</version>
29+
</dependency>
30+
31+
<!-- Add Servlet support for
32+
Spring's AbstractAnnotationConfigDispatcherServletInitializer -->
33+
<dependency>
34+
<groupId>javax.servlet</groupId>
35+
<artifactId>javax.servlet-api</artifactId>
36+
<version>3.1.0</version>
37+
</dependency>
38+
39+
<!-- Add support for JSP ... get rid of Eclipse error -->
40+
<dependency>
41+
<groupId>javax.servlet.jsp</groupId>
42+
<artifactId>javax.servlet.jsp-api</artifactId>
43+
<version>2.3.1</version>
44+
</dependency>
45+
46+
</dependencies>
47+
48+
<!-- Support for Maven WAR Plugin -->
49+
50+
<build>
51+
<finalName>spring-rest-demo</finalName>
52+
53+
<pluginManagement>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-war-plugin</artifactId>
58+
<version>3.2.0</version>
59+
</plugin>
60+
</plugins>
61+
</pluginManagement>
62+
</build>
63+
64+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.luv2code.springdemo.config;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
7+
8+
@Configuration
9+
@EnableWebMvc
10+
@ComponentScan("com.luv2code.springdemo")
11+
public class DemoAppConfig implements WebMvcConfigurer {
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.luv2code.springdemo.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class MySpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
7+
@Override
8+
protected Class<?>[] getRootConfigClasses() {
9+
// TODO Auto-generated method stub
10+
return null;
11+
}
12+
13+
@Override
14+
protected Class<?>[] getServletConfigClasses() {
15+
return new Class[] { DemoAppConfig.class };
16+
}
17+
18+
@Override
19+
protected String[] getServletMappings() {
20+
return new String[] { "/" };
21+
}
22+
23+
}
24+
25+
26+
27+
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.luv2code.springdemo.entity;
2+
3+
public class Student {
4+
5+
private String firstName;
6+
private String lastName;
7+
8+
public Student() {
9+
10+
}
11+
12+
public Student(String firstName, String lastName) {
13+
this.firstName = firstName;
14+
this.lastName = lastName;
15+
}
16+
17+
public String getFirstName() {
18+
return firstName;
19+
}
20+
21+
public void setFirstName(String firstName) {
22+
this.firstName = firstName;
23+
}
24+
25+
public String getLastName() {
26+
return lastName;
27+
}
28+
29+
public void setLastName(String lastName) {
30+
this.lastName = lastName;
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.luv2code.springdemo.rest;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
@RequestMapping("/test")
9+
public class DemoRestController {
10+
11+
// add code for the "/hello" endpoint
12+
13+
@GetMapping("/hello")
14+
public String sayHello() {
15+
return "Hello World!";
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.luv2code.springdemo.rest;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.annotation.PostConstruct;
7+
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import com.luv2code.springdemo.entity.Student;
14+
15+
@RestController
16+
@RequestMapping("/api")
17+
public class StudentRestController {
18+
19+
private List<Student> theStudents;
20+
21+
22+
// define @PostConstruct to load the student data ... only once!
23+
24+
@PostConstruct
25+
public void loadData() {
26+
27+
theStudents = new ArrayList<>();
28+
29+
theStudents.add(new Student("Poornima", "Patel"));
30+
theStudents.add(new Student("Mario", "Rossi"));
31+
theStudents.add(new Student("Mary", "Smith"));
32+
}
33+
34+
35+
36+
// define endpoint for "/students" - return list of students
37+
38+
@GetMapping("/students")
39+
public List<Student> getStudents() {
40+
41+
return theStudents;
42+
}
43+
44+
// define endpoint for "/students/{studentId}" - return student at index
45+
46+
@GetMapping("/students/{studentId}")
47+
public Student getStudent(@PathVariable int studentId) {
48+
49+
// just index into the list ... keep it simple for now
50+
51+
return theStudents.get(studentId);
52+
53+
}
54+
}
55+
56+
57+
58+
59+
60+
61+
62+
63+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<html>
2+
<body>
3+
4+
<h3>Spring REST Demo</h3>
5+
6+
<hr>
7+
8+
<a href="${pageContext.request.contextPath}/test/hello">Hello</a>
9+
10+
<br><br>
11+
12+
<a href="${pageContext.request.contextPath}/api/students">Get All Students</a>
13+
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)