`
kakaluyi
  • 浏览: 437555 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

折腾了一把 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 
//

分享到:
评论

相关推荐

    jax-ws webservice demo

    基于jax-ws 实现的web service client和server端的demo程序。 注:如果使用的是 myeclipse 时 server 部署到tomcat 启动的时候会报错 解决办法:找到myeclipse安装目录下的 plugins 目录里 查找 webservices-rt.jar,...

    metro-jax-ws-master

    The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services, particularly SOAP services.... It's a part of the Java SE and Java EE platforms.

    JAX-WS自学笔记

    1、JAX-WS概述 2、创建Web Service 2.1 从java开始 2.1.1 运行wsgen 2.1.2 生成的WSDL和XSD 2.1.3 目录结构 2.2 从WSDL开始 2.2.1 运行wsimport 2.2.2 生成的java代码 2.3发布Web Service 2.3.1在...

    JAX-WS 2.2 RI所有相关jar包

    JAX-WS 2.2 RI 所包含的JAR包集合,包含25个JAR包,列表如下: FastInoset.jar gmbal-api-only.jar ha-api.jar javax.annotation.jar javax.mail_1.4.jar jaxb-api.jar jaxb-impl.jar jaxb-xjc.jar jaxws-api...

    Jax-ws所需要的JAR包

    亲测可用,Jax-ws所需要的JAR包,拷贝到tomcat安装路径的lib里,实现了webservice发布到tomcat,赞!

    MyEclipse8_0中使用 JAX-WS 部署 WebService 实例

    MyEclipse8_0中使用 JAX-WS 部署 WebService 实例 - 外衣 - 博客频道 - CSDN_NET.mht

    JAX-WS2.0 API

    JAX-WS2.0 API

    JAX-WS_WebService.rar

    JAX-WS方式开发和部署webservice应用,JAX-WS方式开发和部署webservice应用,JAX-WS方式开发和部署webservice应用,JAX-WS方式开发和部署webservice应用,JAX-WS方式开发和部署webservice应用

    java实验_JAX-Ws

    可以更好的了解 jax-ws的实验过程。了解其原理

    Jax-WS 简单实例

    Jax-WS的简单实例 Jax-WS的简单实例

    webService部署tomcat需要的jax-ws jar包

    webService部署tomcat需要的jax-ws 的完整jar包

    jax-ws webservice简单demo

    jax-ws webservice完整demo,包含所有jax-ws 2.2jar包。

    jax-rs jax-ws所需包,亲测可用

    javax.xml.ws.Service 报错需要的包,亲测可以用,直接下载在ide里buildpath一下就可以,四个jar包 ,整合了其他的jar所以配置简单

    JAX-WS开发的文件生成与部署相关全视频过程

    如果基于一个JAX-WS进行WebService开发,有很多教程,但是具体怎么更自动地生成一些文件,实现客户端与服务端的交互,都讲得不大清楚,为了让大家更方便地部署,我将服务端、客户端文件的生成与部署全过程以及测试...

    JAX-WS 2.2 完整jar包

    JAX-WS 2.2 RI 所包含的JAR包集合,包含25个JAR包,列表如下: FastInoset.jar gmbal-api-only.jar ha-api.jar javax.annotation.jar javax.mail_1.4.jar jaxb-api.jar jaxb-impl.jar jaxb-xjc.jar jaxws-api...

    使用JAX-WS(JWS)发布WebService

    使用JAX-WS(JWS)发布WebService 使用myeclipse开发java的webservice的两种方式 方式一: (此方式只能作为调试,有以下bug:jdk1.6u17?以下编译器不支持以Endpoint.publish方式发布document方式的soap,必须在...

    JAX-WS2.2.6包

    JAX-WS规范是一组XML web services的JAVA API

    [Jax-RS] RESTful Java 开发 (Jax-RS 实现) (英文版)

    Learn how to design and develop distributed web services in Java using RESTful architectural principals and the JAX-RS specification in Java EE 6. With this hands-on reference, you'll focus on ...

    JAX-WS Web service

    JAX-WS Web service 开发初步

Global site tag (gtag.js) - Google Analytics