-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1595ef3
commit 8d68ac7
Showing
8 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/build/ | ||
/target/ | ||
/Users/ | ||
/.settings/ | ||
/.classpath | ||
/.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Class-Path: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<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>SpringMVCJSON</groupId> | ||
<artifactId>SpringMVCJSON</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>war</packaging> | ||
<build> | ||
<sourceDirectory>src</sourceDirectory> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<version>3.2.1</version> | ||
<configuration> | ||
<warSourceDirectory>WebContent</warSourceDirectory> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.7.0</version> | ||
<configuration> | ||
<source>10</source> | ||
<target>10</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
<version>5.1.3.RELEASE</version> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>4.0.1</version> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>2.9.8</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
22 changes: 22 additions & 0 deletions
22
SpringMVCJSON/src/config/DispatcherServletInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package config; | ||
|
||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; | ||
|
||
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { | ||
|
||
@Override | ||
protected Class<?>[] getRootConfigClasses() { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected Class<?>[] getServletConfigClasses() { | ||
return new Class[] { JavaBasedConfiguration.class }; | ||
} | ||
|
||
@Override | ||
protected String[] getServletMappings() { | ||
return new String[] { "/" }; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package config; | ||
|
||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
@EnableWebMvc | ||
@ComponentScan(basePackages = { "controller","service" }) | ||
public class JavaBasedConfiguration implements WebMvcConfigurer { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package controller; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import model.Employee; | ||
import service.EmployeeService; | ||
|
||
// @Controller | ||
// @ResponseBody // use @ResponseBody annotation for JSON response. | ||
|
||
// or @RestController | ||
|
||
@RestController | ||
@RequestMapping("/employees") | ||
public class EmployeeController { | ||
|
||
@Autowired | ||
private EmployeeService employeeService; | ||
|
||
@RequestMapping | ||
public List<Employee> getAllEmployees() { | ||
return employeeService.inMemoryData(); | ||
} | ||
|
||
@RequestMapping(value = "/{id}") | ||
public Employee getEmployeeById(@PathVariable("id") int id) { | ||
// return employeeService.inMemoryData().stream().filter(e -> e.getId() == | ||
// id).findFirst().get(); | ||
|
||
for (Employee e : employeeService.inMemoryData()) { | ||
if (e.getId() == id) { | ||
return e; | ||
} | ||
} | ||
throw new RuntimeException("Not Found"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package model; | ||
|
||
public class Employee { | ||
|
||
private int id; | ||
private String name; | ||
private String surname; | ||
private int salary; | ||
|
||
public Employee() { | ||
super(); | ||
} | ||
|
||
public Employee(int id, String name, String surname, int salary) { | ||
super(); | ||
this.id = id; | ||
this.name = name; | ||
this.surname = surname; | ||
this.salary = salary; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getSurname() { | ||
return surname; | ||
} | ||
|
||
public void setSurname(String surname) { | ||
this.surname = surname; | ||
} | ||
|
||
public int getSalary() { | ||
return salary; | ||
} | ||
|
||
public void setSalary(int salary) { | ||
this.salary = salary; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import model.Employee; | ||
|
||
@Service | ||
public class EmployeeService { | ||
|
||
public List<Employee> inMemoryData() { | ||
|
||
Employee employee1 = new Employee(1, "name1", "surname1", 1000); | ||
Employee employee2 = new Employee(2, "name2", "surname2", 2000); | ||
Employee employee3 = new Employee(3, "name3", "surname3", 3000); | ||
|
||
return List.of(employee1, employee2, employee3); | ||
} | ||
} |