조건

  • egovframe.rte.version : 4.1.0
  • 기존 환경에 RESTful API를 제공할 수 있도록 환경 변경
  • restful URL은 "/api/..."로 고정

 

프로젝트 구조

 

 

 

 

web.xml

  • /src/main/webapp/WEB-INF/web.xml
<!-- 기존에 있는 것 -->
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 기존의 것을 정확하게 지정 -->
        <param-value>/WEB-INF/config/egovframework/springmvc/egov-com-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- 
	RESTful 용 : 
    rest란 이름의 DispatcherServlet 추가 생성
-->
 <servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!-- servlet.xml을 분리한 후 정확하게 지정 -->
        <param-value>/WEB-INF/config/egovframework/springmvc/egov-api-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

 

 

context-security.xml 수정

  • /src/main/resources/egovframework/spring/com/context-security.xml
<security:http pattern="/css/**" security="none"/>
<security:http pattern="/html/**" security="none"/>
<security:http pattern="/images/**" security="none"/>
<security:http pattern="/js/**" security="none"/>
<security:http pattern="/resource/**" security="none"/>
<!-- 추가 -->
<security:http pattern="/api/**" security="none"/>

 

 

 

egov-api-servlet.xml 신규 생성

  • /src/main/webapp/WEB-INF/config/egovframework/springmvc/egov-api-servlet.xml
  • 추가 내용
    • 테스트 결과 static resource가 정상적으로 mapping 되지 않는 현상이 발생함
    • jsp에서 /css/...를 호출하면 이게 rest Dispatcher가 처리하는데 매핑 관계 정보가 없어서 no mapping이 나타나는 것으로 보임
    • 매핑 관계 설정
<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="biz.api" >
    	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
    
    <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
	<mvc:resources mapping="/images/**" location="/images/" ></mvc:resources>
	<mvc:resources mapping="/js/**" location="/js/" ></mvc:resources>
    
    <!-- 기본 메시지 converter 자동 추가 --> 
    <mvc:annotation-driven />
    
</beans>

 

 

 

pom.xml 수정

	<-- jackson 추가 -->
	<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.14.3</version>
    </dependency>

</dependencies>

 

 

 

 

+ Recent posts