折腾了一把 JAX-WS, SOA & Java EE 5 (part 1 of 3)

从Servlet到EJB,从J2EE到JAVA EE5,作为JAVA开发人员,需要不断折腾。

最近下载了GlassFish2,折腾了折腾ejb3和web service,现在小结一下。主要总结一下几点:

1,GlassFish2 很好使
2,Jaxb,Web services 和 SOA
3,利用JAXB将XML SCHEMA编译为Java源文件
4,把POJO或EJB搞成(呵呵)Web Service
5,测试程序
6,ClassLoader 与打包部署 .ear
7,两本Java EE 5 书籍读后感

看起来内容很广,小结要简明扼要。

1,GlassFish2 很好使
一句话:GlassFish的管理网页(admin console)使得部署简单方便。这是JBOSS没法儿比的。

2,Jaxb,Xml Schema,Web Service & SOA - 基本概念
本人很讨厌Web Service,但随着SOA(Service Oriented Architecture)的兴起,Web Service是Java开发人员
无法避免的领域。SOA为系统集成提供了较好的架构,而Web Service借助于xml的数据跨平台特性,成为目今较流行
的实现SOA的一种手段。Web Service又有两种流派:一是基于SOAP的;一是REST的。行业内较流行的是基于SOAP的
Web Services,本文也着重小结于SOAP Web Services。

SOAP Web Services 包括几块:HTTP, SOAP, WSDL,XML。HTTP是通讯协议, SOAP是数据的打包协议(from 
developer's point of view), WSDL是Web Service的定义语言,而XML则是数据的载体。

看起来好复杂,其实JAX-WS(or: Java Web Service)很大程度上将大部分任务简化为几个annotation的问题,如
WSDL及其他组件的生成等,都交由服务器来完成。在下面的3中将看到,Java EE 5允许我们将POJO或无状态的EJB搞
成Web Service。

SOAP Web Services意味着service客户必须向服务器提交SOAP消息(SOAP Message),service也将返回SOAP消息。
真正的数据则以XML的格式,按SOAP的打包和编码(Encoding)方式来传送。编程语言如果是Java,则由JAXB负责xml
与Java Object之间的转换:从xml到 Java Object叫作Unmarshalling,从Java Object到xml叫作Marshalling;而
这种转换叫做Data Binding。

在Data Binding中,讲Xml与Java Object联系在一起的中间关键部分是Xml Schema,她定义了一个Xml文件的内容。
虽然一般由业务分析人员(BA: Business Analyst)来定义service payloadrequest/response),但实际上,Web 
Service的设计与实现是从定义service的Schema开始的。这些Schema实际上是项目业务逻辑的抽象描述。因此,掌握
基本的Xml Schema概念,例如Scheam Name Space, built-in types, Complex type, Eelement Occurrences, 
Schema inclusion and extension,Schema validation 等等,是正确有效地实现SOA Web Service所必需的。这
是很多开发人员所忽略的。

复杂的Xml Schema类似一种OOP编程语言,你可以定义一些基本的Schema,如Client,Account等等,而service请求及
响应(Request/Response)的Schema则可以引用或延伸这些基本的Schema。这是Xml Schema的方便与潜力,同时也是
其麻烦与难点。

3,利用JAXB将XML SCHEMA编译为Java源文件
有些人做过Web Service,但未必用过JAXB,因为他们采用的方法是手工解析返回的结果。对于简单的Web Service来
说,这样做是可以的,但对于复杂的SOA Services,这种做法就不现实了。

另一个使用JAXB的原因是,Web Service 的请求与相应参数,不只是简单的基本类型(如int,String etc),而是包
含较多数据的用户自定义类型。真正的SOA service的请求与响应多属于后一种情况。

因此,比较可行的办法就是利用JAXB技术进行Data Binding,以实现Xml与Java Object之间的自动转换。其流程大体
如此:一,定义service request/response 的Schema;然后利用JAXB提供的编码器 xjc(有现成的 ant task)对
Schema进行编码,生成Java源代码,Data Binding的准备就完成了。二,上述生成的源代码,是整个项目源代码的一
部分。Web Service的实现将运用这些源代码。三,使用service时,请求及相应的内容,将符合Schema的要求。

一个简单的例子:假设我们定义Person.xsd及GetPerson.xsd,后者引用前者。GetPerson.xsd定义了一个service的
请求及相应内容:请求提供一个Person的数据,service 将返回有新年龄数据的Person。

Schema Person.xsd:
{code}
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:sc="http://www.t50.com/portable" 
    targetNamespace="http://www.t50.com/portable">
    
    <xs:element name="person" type="sc:Person"/>
    
    <xs:complexType name="Person">
        <xs:sequence>
            <xs:element name="firstName" type="xs:string" />
            <xs:element name="lastName" type="xs:string" />
            <xs:element name="age" type="xs:int" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>
{code}

Schema GetPerson.xsd:
{code}
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:sc="http://www.t50.com/portable" 
    targetNamespace="http://www.t50.com/portable">
    
    <!-- include schema Person.xs -->
    <xs:include schemaLocation="Person.xsd" />
    
    <xs:element name="getPersonRequest" type="sc:GetPersonRequest" />
    <xs:element name="getPersonResponse" type="sc:GetPersonResponse" />
    
    <xs:complexType name="GetPersonRequest">
        <xs:annotation>
          <xs:documentation>Service request type</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="person" type="sc:Person" />
        </xs:sequence>
    </xs:complexType>
    
    <xs:complexType name="GetPersonResponse">
        <xs:annotation>
          <xs:documentation>Service response type</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="person" type="sc:Person" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>
{code}

Ant target to comiple schema to Java source:
{code}
  <target name="generate.portables.src" depends="" description="generate portable Java sources from t50 schema...">
    <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask" classpathref="t50.classpath" />        
      <echo message="compile xsd schema to generate t50 portable sources..." />
      <delete dir="${portable.src}" />
      <mkdir dir="${portable.src}" />
        
    <xjc extension="true" destdir="${portable.src}" >
        <schema dir="${t50.schema.dir}" includes="**/*.xsd" />
        <arg value="-nv" />
    </xjc>
  </target>
{code}

Generated Java Sources from schema Person.xsd and GetPerson.xsd
1. Person.java
{code}
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-3354 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2008.06.26 at 04:07:56 PM NZST 
//


package com.t50.portable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for Person complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * <complexType name="Person">
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <sequence>
 *         <element name="firstName" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         <element name="lastName" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         <element name="age" type="{http://www.w3.org/2001/XMLSchema}int"/>
 *       </sequence>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Person", propOrder = {
    "firstName",
    "lastName",
    "age"
})
public class Person {

    @XmlElement(required = true)
    protected String firstName;
    @XmlElement(required = true)
    protected String lastName;
    protected int age;

    /**
     * Gets the value of the firstName property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Sets the value of the firstName property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setFirstName(String value) {
        this.firstName = value;
    }

    /**
     * Gets the value of the lastName property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Sets the value of the lastName property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setLastName(String value) {
        this.lastName = value;
    }

    /**
     * Gets the value of the age property.
     * 
     */
    public int getAge() {
        return age;
    }

    /**
     * Sets the value of the age property.
     * 
     */
    public void setAge(int value) {
        this.age = value;
    }

}
{code}

2. GetPersonRequest.java
{code}
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-3354 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2008.06.26 at 04:07:56 PM NZST 
//


package com.t50.portable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * Service request type
 * 
 * <p>Java class for GetPersonRequest complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * <complexType name="GetPersonRequest">
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <sequence>
 *         <element name="person" type="{http://www.t50.com/portable}Person"/>
 *       </sequence>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "GetPersonRequest", propOrder = {
    "person"
})
@XmlRootElement(name="getPersonRequest")
public class GetPersonRequest {

    @XmlElement(required = true)
    protected Person person;

    /**
     * Gets the value of the person property.
     * 
     * @return
     *     possible object is
     *     {@link Person }
     *     
     */
    public Person getPerson() {
        return person;
    }

    /**
     * Sets the value of the person property.
     * 
     * @param value
     *     allowed object is
     *     {@link Person }
     *     
     */
    public void setPerson(Person value) {
        this.person = value;
    }

}
{code}

3. GetPersonResponse.java
{code}
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-3354 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2008.06.26 at 04:07:56 PM NZST 
//

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值