둘러보기 생략.
 

[참고] Apache CXF를 활용하여 웹서비스 개발 시 클라이언트 호출 방법

프로젝트 문의 사항으로 답변해드린 내용으로 공유하고자 포럼에 등록합니다.
참고하세요.
---------------------------------------------------------------

[질문] Apache CXF를 이용하여 웹서비스를 개발하고 노출 시,
Apache CXF Client API가 아닌, 일반 자바 클라이언트 코드에서 어떻게 접근하여
웹서비스를 호출하고 그 결과를 확인할 수 있는가?

[답변] Anyframe에서 제공하는 CXF Plugin을 install 시키면 Apache CXF를 이용하여
ProductService가 웹서비스로 노출되어 사용되고 있습니다.
이 경우 JAX-WS Frontend를 사용하고 있습니다.

* Server
1. ProductService Interface Class
@WebService
@XmlSeeAlso( { Product.class })
public interface ProductService extends GenericService
...
2. ProductService Implementation Class
@Service("cxfProductService")
public class ProductServiceImpl extends GenericServiceImpl

3. context-cxf.xml
<jaxws:endpoint id="wsServerProductService" implementor="#cxfProductService" address="/productList" />
중략...

위와 같이 서버단 코딩 및 설정을 완료한 후 웹서비스로 구동시킨 후 아래 클라이언트
코드 작성을 진행하도록 한다.(WSDL 파일에 접근하기 위함)

* Client
1. Apache CXF API, Spring Configuration을 이용하지 않고 클라이언트 코드를
작성하기 위해서는 먼저 아래와 같이 wsimport command를 이용하여
getPort method를 갖는 클래스를 생성해야 한다.(WSDL을 통한 port접근 위함)

command: wsimport wsdl 파일 위치 –d c:\app\classfiles –keep –s c:\app\javafiles
ex.) C:\Java\jdk1.6.0_18\bin>wsimport http://localhost:8080/wstest/ws/productList?wsdl -d c:\app\classfiles -keep -s c:\app\javafiles

위 command를 실행시키기위해서는 wsimport tool을 설치해야 하는데
이는 jdk1.6/bin 폴더하위에 있으므로 이를 이용하도록 한다.
(*클래스 생성에만 사용되므로 실제 구동 시에는 jdk1.5 위에서 수행시켜도 무방함)

2. wsimport를 통해 생성된 자바 코드들 중에서 @WebServiceClient Annotation
설정이 된 ProductServiceImplService 클래스만 필요하고 나머지 클래스는 필요하지 않으므로 삭제해도 된다.

3. 클라이언트 코드 내에서 ProductServiceImplService 클래스의 getProductServiceImplPort() 메소드를 통해 ProductService에 접근한 후 원하는 메소드를 호출하여 사용한다.

코드의 모습은 다음과 같다.

public class WSClient {
@WebServiceRef(wsdlLocation = "http://localhost:8080/wstest/ws/productList?wsdl")
static ProductServiceImplService service = new ProductServiceImplService();

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
WSClient client = new WSClient();
client.doTest(args);
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void doTest(String[] args) {
try { // Call Web Service Operation
ProductService port = service.getProductServiceImplPort();

Page response = port.getPagingList(new ProductSearchVO());
System.out.println(response);
} catch (Exception ex) {
ex.printStackTrace();
}
중략...