본문 바로가기

기타

[ SPRING ] CXF 를 이용한 WebService 구현 1

2014.03.18 18:24

일반적으로 웹 서비스를 구축 하기는 쉽지 않은데...

 

(WSDL 이니 뭐니 이런 규격 알아가는게 귀찮고 힘듭니다.)

 

apache 오픈 소스로 CXF 라는 프로젝트가 있는데 이걸 사용해서

 

구축 하면 생각보다 쉽게 구현이 가능 합니다. (요새 대세인 Spring 결합은 당연합니다.)

 

아래 구현은 Client 에서 Server에 구현 되어 있는 클래스를 사용 해서 결과를 받을 수 있는

 

방식 입니다. (Client 에는 interface 만 있습니다.)

 

사용 중요 library 들은

 

spring 3.0.7 버전

wsdl4j 1.6.2 버전

xmlschema 2.0.3 버전

cxf 2.6.2 버전

 

입니다.

 

 

Server 측과 Client 측을 나누어서 글을 쓸 텐데

 

이번 글은 Server 측을 쓰겠습니다.

 

 

 

 

 일단 서버측 파일 구조는 위의 그림과 같습니다.

 

1. web.xml 에 아래와 같은 구문이 포함 되어야 합니다.

 

 <servlet>
  <servlet-name>CXFServlet</servlet-name>
  <servlet-class>
   org.apache.cxf.transport.servlet.CXFServlet
  </servlet-class>
  <load-on-startup>2</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>CXFServlet</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
 

2. applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
 <!-- 해당 내용은 cfx.jar 에 포함되어 있다. -->
  <import resource="classpath:META-INF/cxf/cxf.xml" />
   <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
   <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
 <!-- ********************************** -->


</beans>

 

3. dispatcher-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<task:annotation-driven/> 
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
 <!-- restful 방식 위해 이후 접근된 모든 접근 request 가 적용될 수 있도록 해야 확장자가 없는 url을 적용하여 적용 시킬 수 있다. /***/*** -->

<util:properties id="config" location="classpath:conf/spring/config.properties" />
<context:annotation-config/>
<import resource="classpath:conf/cxf/webservice_cxf.xml" />


</beans>

 

 

4. webservice_cxf.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:jaxws="http://cxf.apache.org/jaxws"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

 

 <jaxws:endpoint
  id="orderProcess"
  implementor="com.incross.order.service.OrderProcessImpl"
  address="/OrderProcess" />
  
</beans>
 

5. OrderProcess.java 는 아래와 같이 구현 되어 있습니다.

 

package com.incross.order.service;

import javax.jws.WebService;

 

@WebService
public interface OrderProcess {
 

 String processOrder(String user);


}

 

6. OrderProcessImpl.java 는 아래와 같이 구현 되어 있습니다.

 

package com.incross.order.service;

import javax.jws.WebService;


@WebService(endpointInterface = "com.incross.order.service.OrderProcess")
public class OrderProcessImpl implements OrderProcess{
 @Override
 public String processOrder(String user){
  
  if(user == null){
   return "error";
  }else if(user.equals("aaa")){
   return "test_return1";
  }else{
   return "test_return2";
  }
  

 }
}

 

 

제대로 구현 되어 있는지 확인은

 

(tomcat 에 localhost 로 8080 port 로 띄운 결과 입니다.)

 

웹브라우저에서 localhost:8080/프로젝트명/OrderProcess?wsdl 를 접속 하면

 

wsdl 형태로 결과가 나오면 정상 구현 된 것입니다. (아래 예제 사진 참고)

 

 

 

본 글은

 

http://www.ibm.com/developerworks/kr/library/ws-pojo-springcxf/ 

 

를 참조 하여 개발 한 소스를 포함 하였습니다.

 

출처 : http://javafactory.tistory.com/entry/SPRING-CXF-%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%9C-WebService-%EA%B5%AC%ED%98%84-1

'기타' 카테고리의 다른 글

무료 영어 공부 사이트  (0) 2017.03.20
AngularJS를 소개합니다.  (0) 2016.10.02
html base tag - domain  (0) 2014.08.01
중고차 고르기 달인편  (0) 2013.12.01
정규식 표현 특수문자  (0) 2013.07.31