JR 精品文章 - 折腾了一把 JAX-WS, SOA & Java EE 5 (part 1 of 3)
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » J2EE综合 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
折腾了一把 JAX-WS, SOA & Java EE 5 (part 1 of 3)
variable 原创   更新:2008-06-30 13:15:44  版本: 1.0   


从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 
//


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 response type
 * 
 * <p>Java class for GetPersonResponse complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * <complexType name="GetPersonResponse">
 *   <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 = "GetPersonResponse", propOrder = {
    "person"
})
@XmlRootElement(name="getPersonResponse")
public class GetPersonResponse {

    @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}

Note:
a) The "GetPersonRequest.java" and "GetPersonResponse.java" are decorated with annotation @XmlRootElement
   This is required because we will need to define "parameterStyle=SOAPBinding.ParameterStyle.BARE" for the
   SOAPBinding properties(@SOAPBinding annotation) of our web service. @see 4, 把POJO或EJB搞成(呵呵)Web Service

b) It might be good idea to update the above classes to implements java.io.Serializable


4,把POJO或EJB搞成(呵呵)Web Service
(to be continued in part 2)


5,测试程序
(to be continued in part 2)

6,ClassLoader 与打包部署 .ear
(to be continued in part 3)


7,两本Java EE 5 书籍读后感
(a) Enterprise JavaBeans 3.0 (5th Edition) 
(b) Java EE 5 Development using GlassFish Application Server 

(to be continued in part 3)


版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     8       0
作者其它文章: 作者全部文章
评论人:smluyi 发表时间: Tue Jul 08 16:39:08 CST 2008
楼主不顶你不厚道啊,谢谢分享
一个字牛,写的很好

这个文章共有 1 条评论
主题: 折腾了一把JAX-WS, SOA & Java EE 5(part 3 of 3) 上一篇文章
返回文章列表 返回〔J2EE综合〕
下一篇文章 主题: 超级简单的数据库连接池(支持多数据源)


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号