
보통의 WebService예제는 vendor specific 으로 구성되어 있지만 JBoss의 특징이라면 "무조건 표준으로 간다"(워낙에 표준 준수가 심해서 가끔은 짜증이 납니다)이므로 JBoss Web Service라지만 다른 엔진 라이브러리만 있다면 아무데서나 구동될 수 있습니다.
Eclipse를 띄우고 일반 웹 애플리케이션 프로젝트를 띄운 후 JBoss library를 프로젝트의 Build Path로 지정합니다. 이 부분에 대해서는 별도 설명을 하지 않습니다.
1. Server 측 코드를 작성합니다.
package com.jboss.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
/**
* This is a webservice class exposing a method called greet which takes a input
* parameter and greets the parameter with hello.
*
* @author Ji-Woong Choi
*/
/*
* @WebService indicates that this is webservice interface and the name
* indicates the webservice name.
*/
@WebService(name = "Hello", targetNamespace="http://client.jboss.com/", serviceName="GreetService")
/*
* @SOAPBinding indicates binding information of soap messages. Here we have
* document-literal style of webservice and the parameter style is wrapped.
*/
//@SOAPBinding(style = SOAPBinding.Style.RPC, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
@SOAPBinding(style = SOAPBinding.Style.RPC, use = SOAPBinding.Use.LITERAL)
public class Hello {
/**
* This method takes a input parameter and appends "Hello" to it and returns
* the same.
*
* @param name
* @return
*/
@WebMethod
public String greet(@WebParam(name = "name")
String name) {
System.out.println("JBoss Web Service was invoked by " + name);
return "Hello!, " + name;
}
}
Web Service Annotation을 이용하여 필요한 메소드들을 구성합니다.
2. Web.xml 파일을 편집합니다. 본 예제에서는 servlet style을 이용하여 처리합니다.
<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>jboss-ws</display-name>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.jboss.webservice.Hello</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
다른 방법으로는 EJB를 이용하여 port proxy를 remote stub을 이용하는 방법이 있지만 web이 테스트하기 보다 단순하므로 이 예제를 이용하겠습니다.
3. 서버에 디플로이 후 Web Service에 대한 deploy상태를 확인합니다.
http://localhost:8080/jbossws/ 라고 입력하면 웹서비스 리스트를 확인할 수 있습니다.

위의 화면에서 "View a list of deployed services"를 클릭하면 웹서비스들의 상태가 나타나게 됩니다.

4. WSDL의 디플로이된 상태를 확인합니다.

5. 이제 클라이언트 코드를 작성합니다. 여기서는 DII방식(Dynamic Invocation Interface) 방식을 이용하여 작성합니다.
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Call;
import javax.xml.namespace.QName;
import java.net.URL;
public class HelloClientDII
{
public static void main(String[] args)
throws Exception
{
String urlstr = "http://localhost:8080/jboss-ws/Hello?wsdl";
String argument = "Ji-Woong";
System.out.println("Contacting webservice at " + urlstr);
URL url = new URL(urlstr);
String ns = "http://client.jboss.com/";
QName qname = new QName(ns, "GreetService");
QName port = new QName(ns, "HelloPort");
QName operation = new QName(ns, "greet");
ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(url, qname);
Call call = service.createCall(port, operation);
System.out.println("hello.hello(" + argument + ")");
System.out.println("output:" + call.invoke(new Object[] {argument}));
}
}
위의 코드를 실행하면 서버에 System console에 적은 내용이 찍히게 되며, 클라이언트에서 응답을 받을 수 있습니다.
다시 한번 "http://localhost:8080/jbossws/services" url을 확인하면 수행된 시간에 대한 결과치를 확인할 수 있습니다.





