Skip to content

Commit

Permalink
Spring MVC simple json example
Browse files Browse the repository at this point in the history
  • Loading branch information
leventerguder committed Jan 5, 2019
1 parent 1595ef3 commit 8d68ac7
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
6 changes: 6 additions & 0 deletions SpringMVCJSON/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/build/
/target/
/Users/
/.settings/
/.classpath
/.project
3 changes: 3 additions & 0 deletions SpringMVCJSON/WebContent/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

54 changes: 54 additions & 0 deletions SpringMVCJSON/pom.xml
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 SpringMVCJSON/src/config/DispatcherServletInitializer.java
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[] { "/" };
}

}
12 changes: 12 additions & 0 deletions SpringMVCJSON/src/config/JavaBasedConfiguration.java
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 {
}
45 changes: 45 additions & 0 deletions SpringMVCJSON/src/controller/EmployeeController.java
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");
}

}
53 changes: 53 additions & 0 deletions SpringMVCJSON/src/model/Employee.java
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;
}
}
20 changes: 20 additions & 0 deletions SpringMVCJSON/src/service/EmployeeService.java
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);
}
}

0 comments on commit 8d68ac7

Please sign in to comment.