GlassFish 3.1.2充满了MOXy(EclipseLink JAXB)

我非常高兴地宣布, EclipseLink JAXB(MOXy)现在是GlassFish 3.1.2中的JAXB( JSR-222 )提供程序。 我要感谢EclipseLink和GlassFish提交者为实现这一目标付出的​​辛勤工作。

在本文中,我将介绍如何利用MOXy创建JAX-WS服务。 在以后的文章中,我将更详细地介绍更多扩展。

可以从以下链接下载GlassFish:



网络服务(JAX-WS)

对于这篇文章,我们将实现一个简单的服务,该服务通过ID查找客户。 由于这只是“ Hello World”类型的示例,因此该服务将始终返回名称为“ Jane Doe”的客户。

package blog.jaxws.service;

import javax.jws.*;
import blog.jaxws.model.Customer;

@WebService
public class FindCustomer {

 @WebMethod
 public Customer findCustomer(int id) {
  Customer customer = new Customer();
  customer.setId(id);
  customer.setFirstName("Jane");
  customer.setLastName("Doe");
  return customer;
 }

}

WEB-INF / sun-jaxws.xml

有多种方法可以将MOXy指定为JAXB提供程序。 我的偏好是使用位于WEB-INF目录中的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='FindCustomer'
        implementation='blog.jaxws.service.FindCustomer'
        url-pattern='/FindCustomerService'
        databinding='eclipselink.jaxb'/>
</endpoints>

模型

当MOXy被指定为JAXB提供程序时,我们可以利用其所有映射扩展。 在此示例中,我们将使用@XmlPath进行基于XPath的映射

package blog.jaxws.model;

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"firstName", "lastName"})
public class Customer {

 @XmlAttribute
 private int id;

 @XmlPath("personal-info/first-name/text()")
 private String firstName;
 
 @XmlPath("personal-info/last-name/text()")
 private String lastName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

}

WSDL

以下是为此服务生成的WSDL:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Metro/2.2-b13 (branches/2.2-6964; 2012-01-09T18:04:18+0000) JAXWS-RI/2.2.6-promoted-b20 JAXWS/2.2 svn-revision#unknown. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Metro/2.2-b13 (branches/2.2-6964; 2012-01-09T18:04:18+0000) JAXWS-RI/2.2.6-promoted-b20 JAXWS/2.2 svn-revision#unknown. -->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.jaxws.blog/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.jaxws.blog/" name="FindCustomerService">
    <types>
        <xsd:schema>
            <xsd:import namespace="http://service.jaxws.blog/" schemaLocation="http://www.example.com:8080/Blog-JAXWS2/FindCustomerService?xsd=1"/>
        </xsd:schema>
    </types>
    <message name="findCustomer">
        <part name="parameters" element="tns:findCustomer"/>
    </message>
    <message name="findCustomerResponse">
        <part name="parameters" element="tns:findCustomerResponse"/>
    </message>
    <portType name="FindCustomer">
        <operation name="findCustomer">
            <input wsam:Action="http://service.jaxws.blog/FindCustomer/findCustomerRequest" message="tns:findCustomer"/>
            <output wsam:Action="http://service.jaxws.blog/FindCustomer/findCustomerResponse" message="tns:findCustomerResponse"/>
        </operation>
    </portType>
    <binding name="FindCustomerPortBinding" type="tns:FindCustomer">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
            <operation name="findCustomer">
                <soap:operation soapAction=""/>
                <input>
                    <soap:body use="literal"/>
                </input>
                <output>
                    <soap:body use="literal"/>
                </output>
        </operation>
    </binding>
    <service name="FindCustomerService">
        <port name="FindCustomerPort" binding="tns:FindCustomerPortBinding">
            <soap:address location="http://www.example.com:8080/Blog-JAXWS/FindCustomerService"/>
        </port>
    </service>
</definitions>

XML模式

下面是为该模型生成的WSDL引用的XML模式。 请注意,它如何包含@XmlPath批注中指定的“个人信息”元素。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is 
 Metro/2.2-b13 (branches/2.2-6964; 2012-01-09T18:04:18+0000) JAXWS-RI/2.2.6-promoted-b20 
 JAXWS/2.2 svn-revision#unknown. -->
<xsd:schema xmlns:ns0="http://service.jaxws.blog/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://service.jaxws.blog/">
 <xsd:complexType name="findCustomerResponse">
  <xsd:sequence>
   <xsd:element name="return" type="ns0:customer" minOccurs="0" />
  </xsd:sequence>
 </xsd:complexType>
 <xsd:complexType name="findCustomer">
  <xsd:sequence>
   <xsd:element name="arg0" type="xsd:int" />
  </xsd:sequence>
 </xsd:complexType>
 <xsd:complexType name="customer">
  <xsd:sequence>
   <xsd:element name="personal-info" minOccurs="0">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="first-name" type="xsd:string"
       minOccurs="0" />
      <xsd:element name="last-name" type="xsd:string"
       minOccurs="0" />
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
  </xsd:sequence>
  <xsd:attribute name="id" type="xsd:int" use="required" />
 </xsd:complexType>
 <xsd:element name="findCustomerResponse" type="ns0:findCustomerResponse" />
 <xsd:element name="findCustomer" type="ns0:findCustomer" />
</xsd:schema>

服务请求

以下是对我们服务的要求:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Header/>
    <S:Body>
        <ns2:findCustomer xmlns:ns2="http://service.jaxws.blog/">
            <arg0>123</arg0>
        </ns2:findCustomer>
    </S:Body>
</S:Envelope>

服务回应

响应利用了我们在Customer类上使用的@XmlPath批注,将firstNamelastName属性映射到XML。

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns0:findCustomerResponse xmlns:ns0="http://service.jaxws.blog/">
            <return id="123">
                <personal-info>
                    <first-name>Jane</first-name>
                    <last-name>Doe</last-name>
                </personal-info>
            </return>
        </ns0:findCustomerResponse>
    </S:Body>
</S:Envelope>

进一步阅读

如果您喜欢这篇文章,那么您可能会对以下内容感兴趣:

参考: GlassFish 3.1.2在我们的JCG合作伙伴 Blaise Doughan的Java XML&JSON Binding博客中充满了MOXy(EclipseLink JAXB)


翻译自: https://www.javacodegeeks.com/2012/04/glassfish-312-is-full-of-moxy.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值