├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── example │ │ ├── DemoApplication.java │ │ ├── controller │ │ └── EmployeeController.java │ │ ├── model │ │ └── Employee.java │ │ ├── repository │ │ └── EmployeeRepository.java │ │ └── service │ │ ├── EmployeeService.java │ │ └── EmployeeServiceImpl.java └── resources │ └── application.properties └── test └── java └── com └── example └── DemoApplicationTests.java /README.md: -------------------------------------------------------------------------------- 1 | # SpringBootRESTfulTutorial 2 | 3 | mvn clean 4 | mvn clean install 5 | 6 | Spring Boot + RESTful Tutorial 7 | https://medium.com/@gustavo.ponce.ch/spring-boot-restful-tutorial-7f0e314242ef#.8rma5qmrm 8 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.example 7 | demo 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | demo 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.2.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-actuator 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-data-jpa 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-web 39 | 40 | 41 | 42 | com.h2database 43 | h2 44 | runtime 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /src/main/java/com/example/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import org.springframework.context.annotation.Bean; 9 | 10 | import com.example.model.Employee; 11 | import com.example.repository.EmployeeRepository; 12 | 13 | @SpringBootApplication 14 | public class DemoApplication { 15 | 16 | private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class); 17 | 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(DemoApplication.class, args); 21 | } 22 | 23 | @Bean 24 | public CommandLineRunner setup(EmployeeRepository employeeRepository) { 25 | return (args) -> { 26 | employeeRepository.save(new Employee("Gustavo", "Ponce", true)); 27 | employeeRepository.save(new Employee("John", "Smith", true)); 28 | employeeRepository.save(new Employee("Jim ", "Morrison", false)); 29 | employeeRepository.save(new Employee("David", "Gilmour", true)); 30 | logger.info("The sample data has been generated"); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/example/controller/EmployeeController.java: -------------------------------------------------------------------------------- 1 | package com.example.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import com.example.model.Employee; 12 | import com.example.service.EmployeeService; 13 | 14 | @RestController 15 | public class EmployeeController { 16 | 17 | @Autowired 18 | private EmployeeService employeeService; 19 | 20 | @RequestMapping(value = "/employee", method = RequestMethod.GET) 21 | public List getEmployees() { 22 | return employeeService.getAllEmployees(); 23 | } 24 | 25 | @RequestMapping(value = "/employee/{id}", method = RequestMethod.GET) 26 | public Employee getEmployee(@PathVariable("id") long id) { 27 | return employeeService.getEmployeeById(id); 28 | } 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/com/example/model/Employee.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.Id; 6 | 7 | @Entity 8 | public class Employee { 9 | 10 | @Id 11 | @GeneratedValue 12 | private Long id; 13 | private String name; 14 | private String lastName; 15 | private boolean active; 16 | 17 | public Employee() { 18 | super(); 19 | } 20 | 21 | public Employee(String name, String lastName, boolean active) { 22 | super(); 23 | this.name = name; 24 | this.lastName = lastName; 25 | this.active = active; 26 | } 27 | 28 | 29 | public Long getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Long id) { 34 | this.id = id; 35 | } 36 | 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public String getLastName() { 46 | return lastName; 47 | } 48 | 49 | public void setLastName(String lastName) { 50 | this.lastName = lastName; 51 | } 52 | 53 | public boolean isActive() { 54 | return active; 55 | } 56 | 57 | public void setActive(boolean active) { 58 | this.active = active; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/example/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import com.example.model.Employee; 7 | 8 | @Repository("employeeRepository") 9 | public interface EmployeeRepository extends JpaRepository { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/example/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.example.service; 2 | 3 | import java.util.List; 4 | 5 | import com.example.model.Employee; 6 | 7 | public interface EmployeeService { 8 | Employee getEmployeeById(long id); 9 | List getAllEmployees(); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/example/service/EmployeeServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.example.model.Employee; 9 | import com.example.repository.EmployeeRepository; 10 | 11 | @Service("employeeService") 12 | public class EmployeeServiceImpl implements EmployeeService { 13 | 14 | @Autowired 15 | EmployeeRepository employeeRepository; 16 | 17 | @Override 18 | public Employee getEmployeeById(long id) { 19 | return employeeRepository.findOne(id); 20 | } 21 | 22 | @Override 23 | public List getAllEmployees() { 24 | return employeeRepository.findAll(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gustavoponce7/SpringBootRESTfulTutorial/fa66b69c09cd0b26aed3c968d739842429d02410/src/main/resources/application.properties -------------------------------------------------------------------------------- /src/test/java/com/example/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------