[Spring] Restful API 사용하기 위한 설정

chael_lo·2022년 7월 16일
0

Spring

목록 보기
7/7

설정하기

servlet에서 servlet-name으로 정의한 이름을 servlet-mapping에 동일하게 넣은 후 url-pattern을 바꿔준다.
servlet file은 자동적으로 WEB-INF 아래에서 찾는다.

기본 예시

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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>egov.sample</display-name>
    <servlet>
		<servlet-name>eGovFrame</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
 	<!-- eGovFrame-servlet.xml파일 사용  -->
	<servlet-mapping>
		<servlet-name>eGovFrame</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

eGovFrame-servlet.xml(생성해야 함)

크게 컴포넌트 스캔부분과 화면처리용 jsp 설정만 추가하였다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

   <context:component-scan base-package="egovframework">
        <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:annotation-driven />
	
	<!-- 화면처리용 JSP 파일명의  prefix, suffix 처리에 대한 mvc 설정  -->
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" p:order="1"
    p:viewClass="org.springframework.web.servlet.view.JstlView"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>

전자 정부(sample 파일들이 있다는 가정) 예시

.do로 불러오는 부분을 다 삭제해주고 servlet 부분을 아래와 같이 변경해준다.

<servlet>
  <servlet-name>eGovFrame</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/dispatcher-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>eGovFrame</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

출처: 전자정부 restful api 세팅

profile
천천히 꾸준히

0개의 댓글