Spring Framework RESTful 지원, REST API, 하이버네이트 실습(Spring, Rest, Hiberna…

 

Spring Framework RESTful 지원, REST API, 하이버네이트 실습(Spring, Rest, Hibernate, RestTemplate)

 

 

** PDF 첨부 파일로 참고 하셔도 됩니다.


6-20-1. Spring Framework RESTful 지원

 

n  REST API는 웹 서비스를 개발하는 표준 방법이 되었고 자바와 관련하여 사용할 수있는 많은 프레임 워크와 라이브러리가 있다. JAX-RS, Restlet, Jersey, RESTEasy, Apache CFX 등이 있지만 Java 개발자는 Spring MVC를 사용하여 RESTful 웹 서비스를 개발하는 것이 좋다.

n  RESTful 웹 서비스를 개발하기 위해 Spring을 사용하는 가장 중요한 이유는 RESTful 웹 서비스를 개발하기 위해 Spring MVC 환경을 사용할 수 있고 새로운 프레임 워크 또는 라이브러리를 배울 필요가 없기 때문이다.

n  Spring3.0부터 시작된 지난 몇몇 버전에서는 REST 지원을 제공하기 위해 Spring MVC에 많은 개선점을 제공했는데, 우선 전용 주석을 제공하는데 @RestController @ResponseStatus를 사용하면 Spring 4.0에서 RESTful 리소스를 더욱 쉽게 개발할 수 있다.

n  RESTful 웹 서비스를 만드는 데 도움이 될뿐만 아니라 REST 리소스를 소비하는 클래스도 제공한다. RestTemplate 클래스를 사용하여 RESTful 리소스를 사용할 수 있다.

n  스프링 프레임워크는 RESTful 웹서비스를 두가지 형태로 지원한다.

ü  MVC에서 ModelAndView를 아용

ü  HTTP Message Converters를 이용

n  ModelAndView 접근 방식은 오래되었고 문서화가 훨씬 쉬우면서도보다 상세하고 구성이 무거움, REST 패러다임을 구형 모델에 넣으려고 하여 애로사항 있음, Spring 팀은 이것을 이해하고 Spring 3.0부터는 REST 지원을 제공했다.

n  HttpMessageConverter와 어노테이션에 기반한 새로운 접근 방식은 훨씬 가볍고 구현하기 쉽다. 구성이 최소화되어 RESTful 서비스에서 기대할 수있는 적절한 기본값을 제공한다.

n  @EnableWebMvc REST의 경우 클래스 패스에 Jackson JAXB 2가 있는지 감지하고 기본 JSON XML 변환기를 자동으로 만들고 등록한다. 어노테이션의 기능은 XML 버전과 동일합니다. <mvc : annotation-driven />

n  Spring MVC에서 컨트롤러는 RESTful 웹 서비스의 백본 인 모든 HTTP 메소드에 대한 요청을 처리 할 수 있다. 예를 들어, 읽기 조작을 수행하는 GET 메소드, 자원을 작성하는 POST 메소드, 자원을 갱신하는 PUT 메소드 및 서버에서 자원을 제거하는 DELETE 메소드를 처리 할 수 ​​있다.

n  REST의 경우 데이터 표현이 매우 중요하므로 스프링 MVC를 사용하면 @ResponseBody 주석 및 다양한 HttpMessgeConverter 구현을 사용하여 뷰 기반 렌더링을 모두 건너 뛸 수 있다. 이를 사용하여 클라이언트에 직접 응답을 보낼 수 있으며 자원 클라이언트는 원하는 형식으로 원하는 자원을 선택해야 한다.

n  Spring 4.0 릴리스에는 RESTful 웹 서비스 개발을 쉽게하기 위해 @RestController라는 전용 주석이 추가 되어 @Controller 대신 @RestController를 컨트롤러 클래스에 사용하면 Spring은 메시지 대화(요청 메소드)를 컨트롤러의 모든 핸들러 메소드에 적용하며 컨트롤러 모든 메소드에 @ResponseBody 어노테이션을 추가 할 필요가 없다. 이것은 또한 코드를 훨씬 깔끔하게 만들며 모든 응답이 브라우저 바디에 직접 쓰여진다.

n  REST 웹 서비스와 일반적인 웹 애플리케이션 간의 주요 차이점 중 하나는 REST API의 요청은 리소스가 URI 자체의 데이터를 식별한다. ( URI/messages/101 이며 GET, POST, DELETE에 따라 다르게 동작한다,  일반적인 웹 응용 프로그램은 매개 변수를 사용한다.  /messages?Id=101.)

n  파라미터가 기억 나면 @RequestParam을 사용하여 쿼리 매개 변수의 값을 가져 오지만 Spring MVC URL 경로에서 데이터를 추출 할 수있는 @PathVariable 주석을 제공한다. 컨트롤러는 매개 변수화 된 URL에 대한 요청을 처리 할 수 ​​있다.

n  RESTful 웹 서비스의 또 다른 주요 측면은 Representation이다. 동일한 리소스가 JSON, XML, HTML 등 다른 형식으로 표현 될 수 있음을 의미한다. 고맙게도 Spring은 여러 뷰 구현과 뷰를 제공한다.

n  HttpMessageConverts를 사용하여 클라이언트가 원하는 형식으로 응답을 변환하는 데 사용되는 @ResponseBody와 마찬가지로 Spring MVC HttpMethodConverter 구현을 사용하여 인바운드 HTTP 데이터를 컨트롤러의 핸들러에 전달 된 Java 객체로 변환하는 @RequestBody를 제공한다.

n  스프링 프레임 워크는 또한 JdbcTemplate과 유사한 템플릿 클래스 인 RestTemplate REST 리소스를 사용할 수있는 JmsTemplate을 제공한다. 이 클래스를 사용하여 RESTful 웹 서비스를 테스트하거나 REST 클라이언트를 개발할 수있다.

n  Spring MVC를 사용하여 Spring Restful Web Services를 작성한 다음 Rest 클라이언트로 테스트 해 보자. 마지막으로 Spring RestTemplate API를 사용하여 Spring Restful 웹 서비스를 호출하는 방법에 대해서도 살펴보자.

n  Spring Jackson JSON 통합을 사용하여 나머지 호출 응답에서 JSON 응답을 보낸다. 스프링 STS IDE에서 스프링 MVC 스켈레톤 코드를 쉽게 생성하고 Restful 아키텍처를 구현한다.

 

 

 

6-20-2. Spring MVC REST 실습(Hibernate, RestTemplate)

 

n  Spring Legacy Project >> Spring MVC Project(르젝트명[W사1]  : restapi, top level package : restapi)

n  pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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/maven-v4_0_0.xsd">

           <modelVersion>4.0.0</modelVersion>

           <groupId>a.b</groupId>

           <artifactId>restapi</artifactId>

           <name>restapi</name>

           <packaging>war</packaging>

           <version>1.0.0-BUILD-SNAPSHOT</version>

           <properties>

                      <java-version>1.6</java-version>

                      <org.springframework-version>4.3.0.RELEASE</org.springframework-version>

                      <org.aspectj-version>1.6.10</org.aspectj-version>

                      <org.slf4j-version>1.6.6</org.slf4j-version>

           </properties>

           <dependencies>

                      <!-- Spring -->

                      <dependency>

                                 <groupId>org.springframework</groupId>

                                 <artifactId>spring-context</artifactId>

                                 <version>${org.springframework-version}</version>

                                 <exclusions>

                                             <!-- Exclude Commons Logging in favor of SLF4j -->

                                             <exclusion>

                                                        <groupId>commons-logging</groupId>

                                                        <artifactId>commons-logging</artifactId>

                                             </exclusion>

                                 </exclusions>

                      </dependency>

                      <dependency>

                                 <groupId>org.springframework</groupId>

                                 <artifactId>spring-webmvc</artifactId>

                                 <version>${org.springframework-version}</version>

                      </dependency>

 

                      <dependency>

                                 <groupId>org.springframework</groupId>

                                 <artifactId>spring-orm</artifactId>

                                 <version>${org.springframework-version}</version>

                      </dependency>

 

                      <!-- Hibernate ORM -->

                      <dependency>

                                 <groupId>org.hibernate</groupId>

                                 <artifactId>hibernate-core</artifactId>

                                 <version>5.2.11.Final</version>

                      </dependency>

 

                      <!-- Hibernate-C3P0 Integration -->

                      <dependency>

                                 <groupId>org.hibernate</groupId>

                                 <artifactId>hibernate-c3p0</artifactId>

                                 <version>5.2.11.Final</version>

                      </dependency>

 

                      <!-- Mysql Connector -->

                      <dependency>

                                 <groupId>mysql</groupId>

                                 <artifactId>mysql-connector-java</artifactId>

                                 <version>6.0.5</version>

                      </dependency>

                      <!-- Jackson API for JSON -->

                      <dependency>

                                 <groupId>com.fasterxml.jackson.core</groupId>

                                 <artifactId>jackson-databind</artifactId>

                                 <version>2.8.7</version>

                      </dependency>

 

                      <!-- AspectJ -->

                      <dependency>

                                 <groupId>org.aspectj</groupId>

                                 <artifactId>aspectjrt</artifactId>

                                 <version>${org.aspectj-version}</version>

                      </dependency>

 

                      <!-- Logging -->

                      <dependency>

                                 <groupId>org.slf4j</groupId>

                                 <artifactId>slf4j-api</artifactId>

                                 <version>${org.slf4j-version}</version>

                      </dependency>

                      <dependency>

                                 <groupId>org.slf4j</groupId>

                                 <artifactId>jcl-over-slf4j</artifactId>

                                 <version>${org.slf4j-version}</version>

                                 <scope>runtime</scope>

                      </dependency>

                      <dependency>

                                 <groupId>org.slf4j</groupId>

                                 <artifactId>slf4j-log4j12</artifactId>

                                 <version>${org.slf4j-version}</version>

                                 <scope>runtime</scope>

                      </dependency>

                      <dependency>

                                 <groupId>log4j</groupId>

                                 <artifactId>log4j</artifactId>

                                 <version>1.2.15</version>

                                 <exclusions>

                                             <exclusion>

                                                        <groupId>javax.mail</groupId>

                                                        <artifactId>mail</artifactId>

                                             </exclusion>

                                             <exclusion>

                                                        <groupId>javax.jms</groupId>

                                                        <artifactId>jms</artifactId>

                                             </exclusion>

                                             <exclusion>

                                                        <groupId>com.sun.jdmk</groupId>

                                                        <artifactId>jmxtools</artifactId>

                                             </exclusion>

                                             <exclusion>

                                                        <groupId>com.sun.jmx</groupId>

                                                        <artifactId>jmxri</artifactId>

                                             </exclusion>

                                 </exclusions>

                                 <scope>runtime</scope>

                      </dependency>

 

                      <!-- @Inject -->

                      <dependency>

                                 <groupId>javax.inject</groupId>

                                 <artifactId>javax.inject</artifactId>

                                 <version>1</version>

                      </dependency>

 

                      <!-- Servlet -->

                      <dependency>

                                 <groupId>javax.servlet</groupId>

                                 <artifactId>servlet-api</artifactId>

                                 <version>2.5</version>

                                 <scope>provided</scope>

                      </dependency>

                      <dependency>

                                 <groupId>javax.servlet.jsp</groupId>

                                 <artifactId>jsp-api</artifactId>

                                 <version>2.1</version>

                                 <scope>provided</scope>

                      </dependency>

                      <dependency>

                                 <groupId>javax.servlet</groupId>

                                 <artifactId>jstl</artifactId>

                                 <version>1.2</version>

                      </dependency>

 

                      <!-- Test -->

                      <dependency>

                                 <groupId>junit</groupId>

                                 <artifactId>junit</artifactId>

                                 <version>4.7</version>

                                 <scope>test</scope>

                      </dependency>

           </dependencies>

           <build>

                      <plugins>

                                 <plugin>

                                             <artifactId>maven-eclipse-plugin</artifactId>

                                             <version>2.9</version>

                                             <configuration>

                                                        <additionalProjectnatures>

                                                                   <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>

                                                        </additionalProjectnatures>

                                                        <additionalBuildcommands>

                                                                   <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>

                                                        </additionalBuildcommands>

                                                        <downloadSources>true</downloadSources>

                                                        <downloadJavadocs>true</downloadJavadocs>

                                             </configuration>

                                 </plugin>

                                 <plugin>

                                             <groupId>org.apache.maven.plugins</groupId>

                                             <artifactId>maven-compiler-plugin</artifactId>

                                             <version>2.5.1</version>

                                             <configuration>

                                                        <source>1.6</source>

                                                        <target>1.6</target>

                                                        <compilerArgument>-Xlint:all</compilerArgument>

                                                        <showWarnings>true</showWarnings>

                                                        <showDeprecation>true</showDeprecation>

                                             </configuration>

                                 </plugin>

                                 <plugin>

                                             <groupId>org.codehaus.mojo</groupId>

                                             <artifactId>exec-maven-plugin</artifactId>

                                             <version>1.2.1</version>

                                             <configuration>

                                                        <mainClass>org.test.int1.Main</mainClass>

                                             </configuration>

                                 </plugin>

                                 <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-war-plugin</artifactId>

            <version>2.6</version>

            <configuration>

                <!—web.xml 없이 프로젝트를 운영하기 위해 à

                <failOnMissingWebXml>false</failOnMissingWebXml>

            </configuration>

        </plugin>

                      </plugins>

           </build>

</project>

 

n  /src/main/resources/db.properties

 

# MySQL properties

mysql.driver=com.mysql.cj.jdbc.Driver

mysql.url=jdbc:mysql://localhost:3306/restapi?serverTimezone=UTC&createDatabaseIfNotExist=true

mysql.user=root

mysql.password=1234

 

# Hibernate properties

hibernate.show_sql=true

hibernate.hbm2ddl.auto=create-drop

 

#C3P0 properties

hibernate.c3p0.min_size=5

hibernate.c3p0.max_size=20

hibernate.c3p0.acquire_increment=1

hibernate.c3p0.timeout=1800

hibernate.c3p0.max_statements=150

 

n  /src/main/resources/import.sql(Emp 테이블에 초기 데이터 삽입)

 

insert into emp values (1,'1길동',5000000);

insert into emp values (2,'2길동',6000000);

insert into emp values (3,'3길동',7000000);

insert into emp values (4,'4길동',5500000);

insert into emp values (5,'5길동',4500000);

 

n  restapi.config.AppConfig.java

 

package restapi.config;

 

import static org.hibernate.cfg.AvailableSettings.C3P0_ACQUIRE_INCREMENT;

import static org.hibernate.cfg.AvailableSettings.C3P0_MAX_SIZE;

import static org.hibernate.cfg.AvailableSettings.C3P0_MAX_STATEMENTS;

import static org.hibernate.cfg.AvailableSettings.C3P0_MIN_SIZE;

import static org.hibernate.cfg.AvailableSettings.C3P0_TIMEOUT;

import static org.hibernate.cfg.AvailableSettings.DRIVER;

import static org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO;

import static org.hibernate.cfg.AvailableSettings.PASS;

import static org.hibernate.cfg.AvailableSettings.SHOW_SQL;

import static org.hibernate.cfg.AvailableSettings.URL;

import static org.hibernate.cfg.AvailableSettings.USER;

 

import java.util.Properties;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.ComponentScans;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

import org.springframework.core.env.Environment;

import org.springframework.orm.hibernate5.HibernateTransactionManager;

import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import org.springframework.transaction.annotation.EnableTransactionManagement;

 

@Configuration

@PropertySource("classpath:db.properties")

@EnableTransactionManagement // 트랜잭션 가능하도록

@ComponentScans(value = { @ComponentScan("restapi.repository"), @ComponentScan("restapi.service") })

public class AppConfig {

 

           @Autowired

           private Environment env;

 

           @Bean

           public LocalSessionFactoryBean getSessionFactory() {

                      LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();

 

                      Properties props = new Properties();

                      // Setting JDBC properties

                      props.put(DRIVER, env.getProperty("mysql.driver"));

                      props.put(URL, env.getProperty("mysql.url"));

                      props.put(USER, env.getProperty("mysql.user"));

                      props.put(PASS, env.getProperty("mysql.password"));

 

                      // Setting Hibernate properties

                      props.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));

                      props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));

 

                      // Setting C3P0 properties

                      props.put(C3P0_MIN_SIZE, env.getProperty("hibernate.c3p0.min_size"));

                      props.put(C3P0_MAX_SIZE, env.getProperty("hibernate.c3p0.max_size"));

                      props.put(C3P0_ACQUIRE_INCREMENT, env.getProperty("hibernate.c3p0.acquire_increment"));

                      props.put(C3P0_TIMEOUT, env.getProperty("hibernate.c3p0.timeout"));

                      props.put(C3P0_MAX_STATEMENTS, env.getProperty("hibernate.c3p0.max_statements"));

 

                      factoryBean.setHibernateProperties(props);

                      factoryBean.setPackagesToScan("restapi.model");

 

                      return factoryBean;

           }

 

           @Bean

           public HibernateTransactionManager getTransactionManager() {

                      HibernateTransactionManager transactionManager = new HibernateTransactionManager();

                      transactionManager.setSessionFactory(getSessionFactory().getObject());

                      return transactionManager;

           }

}

 

n  restapi.config.DispatcherConfig.java(디스패처 서블릿의 설정을 대체)

 

package restapi.config;

 

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.ComponentScans;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

 

@Configuration

@EnableWebMvc

@ComponentScans(value = { @ComponentScan("restapi.controller") })

public class DispatcherConfig extends WebMvcConfigurerAdapter {

}

 

 

n  restapi.config.MyWebInitializer.java(web.xml을 대체)

 

package restapi.config;

 

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

 

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

       @Override

       protected Class<?>[] getRootConfigClasses() {

             return new Class[] { AppConfig.class };

       }

 

       @Override

       protected Class<?>[] getServletConfigClasses() {

             return new Class[] { DispatcherConfig.class };

       }

 

       @Override

       protected String[] getServletMappings() {

             return new String[] { "/" };

       }

}

 

n  restapi.model.Emp.java(롬복 사용)

 

package restapi.model;

 

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

 

import lombok.Data;

 

@Data

@Entity(name = "Emp")   //Emp라는 이름으로 respapi DB에 데이블을 생성

public class Emp {

           @Id

           @GeneratedValue(strategy = GenerationType.IDENTITY)

           private Long empno;

           private String ename;

           private Integer sal;

}

 

n  restapi.repository.EmpDao.java

 

package restapi.repository;

 

import java.util.List;

 

import restapi.model.Emp;

 

public interface EmpDao {

           long save(Emp emp);

 

           Emp get(long empno);

 

           List<Emp> list();

 

           void update(long empno, Emp emp);

 

           void delete(long empno);

}

 

n  restapi.repository.EmpDaoImpl.java

 

package restapi.repository;

 

import java.util.List;

 

import javax.persistence.criteria.CriteriaBuilder;

import javax.persistence.criteria.CriteriaQuery;

import javax.persistence.criteria.Root;

 

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

 

import restapi.model.Emp;

 

@Repository

public class EmpDaoImpl implements EmpDao {

 

           @Autowired

           private SessionFactory sf;

 

           @Override

           public long save(Emp emp) {

                      sf.getCurrentSession().save(emp);

                      return emp.getEmpno();

           }

 

           @Override

           public Emp get(long empno) {

                      return sf.getCurrentSession().get(Emp.class, empno);

           }

 

           @Override

           public List<Emp> list() {

                      Session session = sf.getCurrentSession();

                      CriteriaBuilder cb = session.getCriteriaBuilder();

                      CriteriaQuery<Emp> cq = cb.createQuery(Emp.class);

                      Root<Emp> root = cq.from(Emp.class);

                      cq.select(root);

                      Query<Emp> query = session.createQuery(cq);

 

                      return query.getResultList();

           }

 

           @Override

           public void update(long empno, Emp emp) {

                      Session session = sf.getCurrentSession();

                      Emp emp2 = session.byId(Emp.class).load(empno);

                      emp2.setEname(emp.getEname());

                      emp2.setSal(emp.getSal());

                      session.flush();

           }

 

           @Override

           public void delete(long empno) {

                      Session session = sf.getCurrentSession();

                      Emp emp = session.byId(Emp.class).load(empno);

                      session.delete(emp);

           }

}

 

n  restapi.service.EmpService.java

 

package restapi.service;

 

import java.util.List;

 

import restapi.model.Emp;

 

public interface EmpService {

           long save(Emp emp);

 

           Emp get(long empno);

 

           List<Emp> list();

 

           void update(long empno, Emp emp);

 

           void delete(long empno);

}

 

n  restapi.service.EmpServiceImpl.java

 

package restapi.service;

 

import java.util.List;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

 

import restapi.model.Emp;

import restapi.repository.EmpDao;

 

@Service

@Transactional(readOnly = true)

public class EmpServiceImpl implements EmpService {

 

       @Autowired

       private EmpDao empDao;

 

       @Override

       public long save(Emp emp) {

             return empDao.save(emp);

       }

 

       @Override

       public Emp get(long empno) {

             return empDao.get(empno);

       }

 

       @Override

       public List<Emp> list() {

             return empDao.list();

       }

 

       @Override

       @Transactional(readOnly = false)

       public void update(long empno, Emp emp) {

             empDao.update(empno, emp);

       }

 

       @Override

       @Transactional(readOnly = false)

       public void delete(long empno) {

             empDao.delete(empno);

       }

 

}

 

n  restapi.controller.EmpController.java

 

package restapi.controller;

 

import java.util.List;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

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

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestBody;

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

 

import restapi.model.Emp;

import restapi.service.EmpService;

 

@RestController

public class EmpController {

       @Autowired

       private EmpService empService;

 

       // 신규 사원 추가

       @PostMapping("/emp")

       public ResponseEntity<String> save(@RequestBody Emp emp) {

             long empno = empService.save(emp);

             return ResponseEntity.ok().body("New Emp has been saved with EMPNO:" + empno);

       }

 

       // empno 사원 조회

       @GetMapping("/emp/{empno}")

       public ResponseEntity<Emp> get(@PathVariable("empno") long empno) {

             Emp emp = empService.get(empno);

if (emp == null) {

                    System.out.println("Emp with empno " + empno + " not found!");

                    return new ResponseEntity<Emp>(HttpStatus.NOT_FOUND);

             }

             return ResponseEntity.ok().body(emp);

       }

 

       // 모든 사원 조회

       @GetMapping("/emp")

       public ResponseEntity<List<Emp>> list() {

             List<Emp> emps = empService.list();

             return ResponseEntity.ok().body(emps);

       }

 

       // empno, EMP 객체를 받아 해당 사원 수정

       @PutMapping("/emp/{empno}")

       public ResponseEntity<?> update(@PathVariable("empno") long empno, @RequestBody Emp emp) {

             empService.update(empno, emp);

             return ResponseEntity.ok().body("Emp has been updated successfully.");

       }

 

       // empno 받아 사원 삭제

       @DeleteMapping("/emp/{empno}")

       public ResponseEntity<?> delete(@PathVariable("empno") long empno) {

             empService.delete(empno);

             return ResponseEntity.ok().body("Emp has been deleted successfully.");

       }

}

 

 

[Chrome Advanced Rest Client를 이용하여 테스트]

 

1.     전체 사원 조회

 

2.     2번 사원 조회

 

 

3.     2번 사원 삭제

 

 

4.      POST 방식으로 한건의 사원데이터를 입력했다.

 

이번에는 RestTemplate을 이용하여 RestClient를 만들어 보자.

 

테스트 전에 앞에서 작성한 프로젝트를 실행하여 TOMCAT을 시작 시키자.

 

1. EmpTest.java

 

package a.b.restapi;

 

import static org.hamcrest.CoreMatchers.equalTo;

import static org.junit.Assert.assertThat;

 

import org.junit.Test;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.web.client.RestTemplate;

 

import restapi.model.Emp;

 

public class EmpTest {

 

       @Test

       public void test() {

             System.out.println("------------------");

             RestTemplate rt = new RestTemplate();

             String url = "http://localhost:8080/restapi/emp";

             ResponseEntity<?> res1 = rt.getForEntity(url + "/3", Emp.class);

             System.out.println(res1);

             assertThat(res1.getStatusCode(), equalTo(HttpStatus.OK));

 

             Emp e1 = new Emp();

             e1.setEname("박길홍");

             e1.setSal(6000000);

 

             String res2 = rt.postForObject(url, e1, String.class);

             System.out.println(res2);

 

             // rt.delete(url + "9999");

 

       }

}

 

 

[결과]

<200 OK,Emp(empno=3, ename=3길동, sal=7000000),{Content-Type=[application/json;charset=UTF-8], Transfer-Encoding=[chunked], Date=[Sun, 10 Jun 2018 08:27:32 GMT]}>

New Emp has been saved with EMPNO : 6 


 

Comments