프로그래밍/XML 관련

JAX-WS를 이용한 쉬운 웹서비스 개발 방법

Terry Cho 2008. 12. 12. 13:59
JAX-WS를 이용해서 웹서비스를 만드는 방법입니다.
영어로 작성했지만 내용은 쉽습니다.

Overview

This is a programming guide for component developer to make a webservice easily.

Technical Standard

This is webservice creation guide for component team.
We will use webservice development standard like this.

  • JAX-WS
  • JAXB

Standard IDE is WebLogic workshop 10.3. You can use any IDE that you want. JAX-WS is standard.WebLogic provides eaisest development environment. If you want to find any alternatives, i want to recommend CXF in Apache. It is easily integrated with Spring framework

Restriction

Here is webservice development restriction for more easy development

  • Synchronous WebService Call
    You can just use synchronous message exchange pattern. If you want to use async,pub-sub, or queuing etc. Please talk with Application Architect.
  • DataTypes in Value Object has a restriction.
    Because of we are using JAXB. We have a restriction to use data type in Value Object that is used as parameter or return type of webservice.
    Here is list of data type you can use it to define ValueObject
    Java Data Types XML Scheme Data Type
    boolean boolean
    byte byte
    double double
    float float
    long long
    int int
    String string
    java.math.BigInteger integer
    java.util.Calendar dateTime
    java.util.Data dateTime

    In addtion you properbly need a use a some of array type. Here is a guide. You have to remember that you cannot use Map or Hashtable data type. You have to covert the types into ArrayList etc.

    Java Data Types XML Scheme Data Type
    java.util.Collection Literal Array
    java.util.List Literal Array
    java.util.ArrayList Literal Array
    java.util.LinkedList Literal Array
    java.util.Vector Literal Array
    java.util.Stack Literal Array
    java.util.Set Literal Array
    java.util.TreeSet Literal Array
    java.utils.SortedSet Literal Array
    java.utils.HashSet Literal Array

Programming Steps

1. Make a ValueObject
Make a ValueObject.You can easily make a ValueObject with datatype decribed above.
You have to use some notation to provide more clarified WSDL generation.

Item.java
package osp.sample.valueObject;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD) @XmlType (propOrder={ "name", "price", "quantity"} )
public class Item { @XmlElement(required=true) String name;
@XmlElement(required=true) int price;
@XmlElement(required=true) int quantity;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getPrice() { return price; }
public void setPrice(int price) { this.price = price; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }

}
  • First import java.xml.bind.annotation.* to use JAXB annotation
  • specify XmlAccessorType with XmlAccessType.FIELD
  • specify XmlType and propOrder. This for sequence of element generated by Java Class.
  • Each of java variable. Specify XmlElement and required=true. This is just for the Xml parsing performance. If the data type is List or Array. You don't have to specify XmlElement

2. Create WebService
Create WebService in WebLogic Workshop IDE and implement a Webservice.

OrderWebService.java
package bcho.sample.webservice;
import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService;

import bcho.sample.valueObject.PurchaseOrder;

@WebService public class OrderWebService {
@WebMethod public @WebResult(name="purchaseOrder")PurchaseOrder addOrder( @WebParam(name="purchaseOrder")PurchaseOrder po)
{ return po; }
}
  • You also import javax.jws.* to use JAX-WS annotation.
  • Use @WebService annotation into a class to declare the class is webservice.
  • Use @WebMethod annotation into method, the method will be automatically generated "operation" in webserivce.
  • Use @WebResult and @WebParam in return value and parameter. As i mentioned earlier, please use one result value and one result parameter.
    The name of @WebResult and @WebMethod should be equals with ClassName.

3. Run a Test
You can test a webservice in WebLogic Workshop IDE.

4. Package and deploy

  • After testing, in WebLogic Workshop . Select a project in Project explorer.
  • Click right button of Mouse and select Export.
  • Select war in export menu. The *.war file automatically generated.
  • Please delpoy the war file into Pilot Project environment (WebLogic Server)

Here is a short tutorial movie of WebService development.
[pilot:JAX-WS based Web Service Programming Guide^WebLogic 10.3 JAX-WS Creation guide.mpeg]

Reference

JAXB Development http://edocs.bea.com/wls/docs103/webserv/data_types.html#wp223908
JAX-WS WebService Development http://edocs.bea.com/wls/docs103/webserv/jws.html#program_jws_file