2014년 7월 28일 월요일

[스프링4 MVC REST예제]Spring4 MVC RESTFul웹서비스,스프링프레임워크4,RestController

[스프링4 MVC REST예제]Spring4 MVC RESTFul웹서비스,스프링프레임워크4,RestController

템플릿을 다운받아서 봐도 되겠지만 직접 만들어 보자.

0. 전체 프로젝트 파일 구성은 아래 그림과 같다.


1. File -> New -> Project -> Spring -> Spring Project -> Simple Spring Web Maven 
   project name은 spring4rest로 하자.(프로젝트 이름이 웹사이트의 컨텍스트 명이다.)


2. pom.xml에서 스프링 버전을 4로 변경하자.(기본적으로 3.2로 되어 있을 것이다)
<spring-framework.version>4.0.2.RELEASE</spring-framework.version>

그런다음 프로젝트에서 마우스 우측버튼 -> maven -> update project를 실행하여 spring4 관련 jar 파일을 내려받자.


<pom.xml>

<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>org.springframework.samples.service.service</groupId>
  <artifactId>spring4rest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
    <properties>

<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Web -->
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<servlet.version>2.5</servlet.version>

<!-- Spring -->
<spring-framework.version>4.0.2.RELEASE</spring-framework.version>

<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>

<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>

<!-- Test -->
<junit.version>4.11</junit.version>

</properties>
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Other Web dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>

<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>

<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>

<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>




3. WEB-INF/web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>spring4rest</display-name>     
    
    <!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>




4. /WEB-INF/mvc-config.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<mvc:annotation-driven />
<!--  컨트롤러등을 찾는 기본 패키지 경로  -->
<context:component-scan base-package="controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>


6. webapps/view/hello.jsp


<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head> 
<body>
<h2>안녕~ ${msg}</h2>
</body>
</html>





7. main/java/controller/RestController.java

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RestController {
@RequestMapping(value="/hello", method=RequestMethod.GET)
public String hello(ModelMap model) {
model.addAttribute("msg", "OracleJava Community Spring4");
return "hello";
}
//아래 hi 메소드로 들어오는 인자값은 URL경로의일부로 넘겨줘야 한다.
//http://localhost:8080/spring4rest/hi/oraclejavacommunity
// ---> msg에 oraclejavacommunity문자열이 할당
@RequestMapping(value="/hi/{msg}", method=RequestMethod.GET)
public String hi(@PathVariable String msg, ModelMap model) {
model.addAttribute("msg",msg);
return "hello";
}
}



8. 실행

http://localhost:8080/spring4rest/hello
http://localhost:8080/spring4rest/hi/OracleJavaCommunity



댓글 없음:

댓글 쓰기