상세 컨텐츠

본문 제목

JavaTM Web Services Developer Pack 2.0 (4)

프로그래밍/웹서비스

by 라제폰 2008. 12. 13. 20:29

본문

JAXWS20 First Sample

 

 

Sample Program : Server

 

 

Web Service Business Class을 작성한다.

 

package fromjava.server;

 

 

public class AddNumbersException extends Exception {

 

    private static final long serialVersionUID = 1L;

 

    String detail;

 

   

 

    public AddNumbersException (String message, String detail) {

 

        super (message);

 

        this.detail = detail;

 

    }

 

   

 

    public String getDetail () {

 

        return detail;

 

    }

 

}

 

 

package fromjava.server;

 

import javax.xml.ws.Endpoint;

 

 

public class AddWebservice {

 

   

 

    public static void main (String[] args) throws Exception {

 

        Endpoint.publish (

 

            "http://localhost:8080/jaxws20test/addnumbers",

 

            new AddNumbersImpl ());

 

    }

 

}

 

 

package fromjava.server;
import javax.jws.WebService;
@WebService
public class AddNumbersImpl {
    
public int addNumbers(int number1, int number2) throws AddNumbersException {
        if (number1 < 0 || number2 < 0) {
            throw new AddNumbersException("Negative number cant be added!",
                "Numbers: " + number1 + ", " + number2);
        }
        return number1 + number2;
    }
}

 

 

Business Class Compile

 

set path=%path%;E:CommonLibjwsdp-2.0jaxwsbin;

 

 

set classpath=%classpath%;E:CommonLibjwsdp-2.0jaxwslibjaxws-api.jar;E:CommonLibjwsdp-2.0jaxwslibjaxws-rt.jar;E:CommonLibjwsdp-2.0jaxwslibjaxws-tools.jar;E:CommonLibjwsdp-2.0jaxwslibjsr181-api.jar;E:CommonLibjwsdp-2.0jaxwslibjsr250-api.jar;E:CommonLibjwsdp-2.0jaxblibjaxb-api.jar;E:CommonLibjwsdp-2.0jaxblibjaxb-impl.jar;E:CommonLibjwsdp-2.0jaxblibjaxb-xjc.jar;E:CommonLibjwsdp-2.0jaxblibjaxb1-impl.jar;

 

 

javac -d  . fromjavaserver*.java

 

 

Generate Portable Artifacts

 

wsgen -cp . -keep -d . -wsdl fromjava.server.AddNumbersImpl

 

 

Create a WAR file to deploy

 

 

web.xml 작성

 

<?xml version="1.0" encoding="UTF-8"?>

 

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

 

<web-app>

 

  <display-name>fromjava</display-name>

 

  <description>fromjava</description>

 

  <listener>

 

    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>

 

  </listener>

 

  <servlet>

 

    <servlet-name>fromjava</servlet-name>

 

    <display-name>fromjava</display-name>

 

    <description>JAX-WS endpoint - fromjava</description>

 

    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>

 

    <load-on-startup>1</load-on-startup>

 

  </servlet>

 

  <servlet-mapping>

 

    <servlet-name>fromjava</servlet-name>

 

    <url-pattern>/addnumbers</url-pattern>

 

  </servlet-mapping>

 

  <session-config>

 

    <session-timeout>60</session-timeout>

 

  </session-config>

 

</web-app>

 

 

 

sun-jaxws.xml 작성

 

<?xml version="1.0" encoding="UTF-8"?>

 

<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>

 

    <endpoint

 

        name='fromjava'

 

        implementation='fromjava.server.AddNumbersImpl'

 

        url-pattern='/addnumbers'/>

 

</endpoints>

 

이 파일은 JAX-RPC 1.x 에서 “jaxrpc-ri.xml” 파일과 유사한 파일이다.

 

 

WAR 작성 및 배포

 

l        WEB-INFclasses : Business Class와 생성한 Artifacts 클래스

 

l        WEB-INFwsdl : 생성된 wsdl, xsd 파일(AddNumbersImplService.wsdl, AddNumbersImplService_schema1.xsd)

 

l        WEB-INF : web.xml, sun-jaxws.xml 파일

 

l        위 파일들을 위치 시킨 후 JWSDP2.0 과 같이 설치한 TomcatForJWSDP에 배포한다.

 

 
Sample Program : Client

 

 

Generate a Service Endpoint Interface

 

wsimport -d . -p addumber.client.gen http://localhost:8080/jaxws20test/addnumbers?wsdl

 

wsimport 명령어는 JAXWSbin에 있다. http://localhost:8080/jaxws20test/addnumbers?wsdl

 

부분은 서버 모듈이 설치되어 있는 WAS의 실제 접근 URL이다. 위 명령어로 생성된 Service Endpoint Interface을 이용하여 Client Program을 작성한다.

 

 

Client Program

 

package fromjava.client;

 

 

import addumber.client.gen.*;

 

import addumber.client.gen.AddNumbersImpl;

 

 

 

public class AddNumbersClient {

 

    public static void main (String[] args) {

 

        try {

 

            AddNumbersImpl port = new AddNumbersImplService().getAddNumbersImplPort();

 

           

 

            int number1 = 10;

 

            int number2 = 20;

 

           

 

            System.out.printf ("Invoking addNumbers(%d, %d)n", number1, number2);

 

            int result = port.addNumbers (number1, number2);

 

            System.out.printf ("The result of adding %d and %d is %d.nn", number1, number2, result);

 

           

 

            number1 = -10;

 

            System.out.printf ("Invoking addNumbers(%d, %d)n", number1, number2);

 

            result = port.addNumbers (number1, number2);

 

            System.out.printf ("The result of adding %d and %d is %d.n", number1, number2, result);

 

        } catch (AddNumbersException_Exception ex) {

 

            System.out.printf ("Caught AddNumbersException_Exception: %sn", ex.getFaultInfo ().getDetail ());

 

        }

 

    }

 

}

 

 

Compile and Run

 

Compile 및 실행시에 다음 libraryClasspath에 잡혀 있어야 한다. 특히 SAAJJWSDP2.0에 포함되어 있는 SAAJ 1.3 버전이 어야 한다.

 

l        JAXB

 

l        JAXP

 

l        JAXWS

 

l        JWSDP_SHARE

 

l        SAAJ

 

l        SJSXP

 

실행

 

java fromjava.client.AddNumbersClient

 

 

nvoking addNumbers(10, 20)

 

The result of adding 10 and 20 is 30.

 

 

Invoking addNumbers(-10, 20)

 

Caught AddNumbersException_Exception: Numbers: -10, 20

 

 


관련글 더보기