Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework#6 by North #48

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,18 @@ In pull request mark your mentor (@mentorName) so it can review it.
To complite task one you need to fix DataProcessorTest unit tests.
All methods in DataProcessor with "7" in a name should be implemented using Java 7, when other should be done with Java 8 streaming api.


## Hometask 6

To implement the REST service in EmployeeController for modal class Employee with EmployeeRepository as DAO, service functionalities including:
//todo add Restful services to getAll

//todo add Restful services to get

//todo add Restful services to getByName

//todo add Restful services to create

//todo add Restful services to put, response 404(Not Found) when encounter the exception that the id is not exist

//todo add Restful services to delete
90 changes: 60 additions & 30 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,84 @@
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>com.epam</groupId>
<artifactId>java-mentoring-program</artifactId>
<groupId>EPAM</groupId>
<artifactId>RESTPractice</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<name>demoRest</name>
<description>Practice project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.5</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.5</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.2</version>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>13.0.1</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>13.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework</groupId>-->
<!--<artifactId>spring-web</artifactId>-->
<!--<version>4.2.6.RELEASE</version>-->
<!--</dependency>-->
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
</project>
45 changes: 45 additions & 0 deletions src/main/java/com/epam/mentoring/restapi/DemoRestApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.epam.mentoring.restapi;

import com.epam.mentoring.restapi.modal.Department;
import com.epam.mentoring.restapi.modal.Employee;
import com.epam.mentoring.restapi.repository.DepartmentRepository;
import com.epam.mentoring.restapi.repository.EmployeeRepository;
import com.epam.mentoring.restapi.web.SecurityFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoRestApplication {

public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication();
ApplicationContext ctx =
SpringApplication.run(DemoRestApplication.class, args);
initDB(ctx);
}

private static void initDB(ApplicationContext ctx){
DepartmentRepository deptRepository= (DepartmentRepository) ctx.getBean(DepartmentRepository.class);
deptRepository.save(new Department("HR", "Human Resource"));
deptRepository.save(new Department("DEV", "Development"));


EmployeeRepository employeeRepository = (EmployeeRepository) ctx.getBean(EmployeeRepository.class);
employeeRepository.save(new Employee("name", "code", 1L, null));
}

//
// @Bean
// public FilterRegistrationBean filterRegistrationBean() {
// FilterRegistrationBean registrationBean = new FilterRegistrationBean();
// SecurityFilter securityFilter = new SecurityFilter();
// registrationBean.setFilter(securityFilter);
// registrationBean.addUrlPatterns("/api/*");
//
// return registrationBean;
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.epam.mentoring.restapi.controller;

import com.epam.mentoring.restapi.modal.Department;
import com.epam.mentoring.restapi.repository.DepartmentRepository;
import com.epam.mentoring.restapi.web.exception.BadRequestError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: Francis
* Date: 12-5-13
* Time: 上午8:42
* To change this template use File | Settings | File Templates.
*/
@RestController
@RequestMapping(value = "/api/")
public class DepartmentController {
private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DepartmentController.class);
@Autowired
private DepartmentRepository repository;

@RequestMapping(method = RequestMethod.GET, value = "/departments")
public
@ResponseBody
List<Department> getDepartments() {
List<Department> list = repository.findAll();
return list;
}

@RequestMapping(method = RequestMethod.GET, value = "/departments/{id}")
public
@ResponseBody
Department getDepartment(@PathVariable long id) {
Department department = repository.findOne(id);
return department;
}


@RequestMapping(method = RequestMethod.POST, value = "/departments")
public
@ResponseBody
Long save(@RequestBody Department s) {
LOG.info("creating Department: " + s.getName());
//validate(s, false);
repository.save(s);
return s.getId();
}

@RequestMapping(method = RequestMethod.PUT, value = "/departments/{id}")
public
@ResponseBody
void update(@RequestBody Department s, @PathVariable long id) {
LOG.info("update Department: " + id + "," + s.getName());
if (s.getId() != id)
throw new BadRequestError("id is not match");
repository.save(s);
}

@RequestMapping(method = RequestMethod.DELETE, value = "/departments/{id}")
public
@ResponseBody
void delete(@PathVariable long id) {
LOG.info("delUser: " + id);
repository.delete(id);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.epam.mentoring.restapi.controller;

import java.util.ArrayList;
import java.util.List;

import javax.xml.ws.http.HTTPException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.epam.mentoring.restapi.modal.Employee;
import com.epam.mentoring.restapi.repository.EmployeeRepository;

/**
* Created by pengfrancis on 16/5/19.
*/
@RestController
@RequestMapping(value = "/api/employee")
public class EmployeeController {

@Autowired
private EmployeeRepository employeeRepository;

// todo add Restful services to getAll
@RequestMapping(method = RequestMethod.GET, value = "")
public @ResponseBody List<Employee> getAll() {
return employeeRepository.findAll();
}

// todo add Restful services to get
@RequestMapping(method = RequestMethod.GET, value = "/{id}")
public @ResponseBody Employee get(@PathVariable Long id) {
return employeeRepository.findOne(id);
}

// todo add Restful services to getByName
@RequestMapping(method = RequestMethod.GET, value = "/name/{name}")
public @ResponseBody List<Employee> getByName(@PathVariable String name) {
return employeeRepository.findByName(name);
}

// todo add Restful services to create,
@RequestMapping(method = RequestMethod.POST, value = "/create")
public void create(@RequestBody Employee employee) {
employeeRepository.save(employee);
}

// todo add Restful services to put, response 404(Not Found) when encounter
// the exception that the id is not exist
@RequestMapping(method = RequestMethod.PUT, value = "/put")
public ResponseEntity put(@RequestBody Employee employee) {
if (employeeRepository.findOne(employee.getId()) == null) {
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}
employeeRepository.save(employee);
return new ResponseEntity<String>(HttpStatus.OK);
}

// todo add Restful services to delete
@RequestMapping(method = RequestMethod.POST, value = "/delete/{id}")
public void delete(@PathVariable Long id) {
employeeRepository.delete(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.epam.mentoring.restapi.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Created by pengfrancis on 16/5/19.
*/
@RestController
public class HelloControllor {

@RequestMapping(value = "hello/{name}")
public String hello(@PathVariable String name){
return "hello, "+ name;
}
}
Loading