通过JAX-WS Provider在Web服务中利用MOXy

在先前的文章中,我演示了如何将EclipseLink JAXB(MOXy)直接集成到WebLogic(从12.1.1开始)和GlassFish(从3.1.2开始)的JAX-WS实现中 。 在本文中,我将演示如何通过使用JAX-WS Provider类在任何应用程序服务器中利用MOXy。

网络服务

JAX-WS中的提供程序机制为您提供了一种创建可直接访问XML的Web服务的方法。 通过@ServiceMode批注,您可以指定是要从消息中获取所有XML还是仅是负载。

FindCustomerService

所有的魔术都发生在invoke方法中。 由于我们将PAYLOAD指定为服务模式,因此输入将是表示消息正文的Source实例。 所有JAXB(JSR-222)实现都可以从Source解组,因此我们将这样做以实现请求。 执行业务逻辑后,我们需要将响应的正文作为Source的实例返回。 为此,我们将响应对象包装在JAXBSource的实例中。

package blog.jaxws.provider;import javax.xml.bind.*;
import javax.xml.bind.util.JAXBSource;
import javax.xml.transform.Source;
import javax.xml.ws.*;@ServiceMode(Service.Mode.PAYLOAD)
@WebServiceProvider(portName = 'FindCustomerPort', serviceName = 'FindCustomerService', targetNamespace = 'http://service.jaxws.blog/', wsdlLocation = 'WEB-INF/wsdl/FindCustomerService.wsdl')
public class FindCustomerService implements Provider<Source> {private JAXBContext jaxbContext;public FindCustomerService() {try {jaxbContext = JAXBContext.newInstance(FindCustomerResponse.class,FindCustomerRequest.class);} catch (JAXBException e) {throw new WebServiceException(e);}}@Overridepublic Source invoke(Source request) throws WebServiceException {try {Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();FindCustomerRequest fcRequest = (FindCustomerRequest) unmarshaller.unmarshal(request);Customer customer = new Customer();customer.setId(fcRequest.getArg0());customer.setFirstName('Jane');customer.setLastName('Doe');FindCustomerResponse response = new FindCustomerResponse();response.setValue(customer);return new JAXBSource(jaxbContext, response);} catch (JAXBException e) {throw new WebServiceException(e);}}}

MOXy作为JAXB提供者

要指定将MOXy用作JAXB提供程序,我们需要包含一个名为jaxb.properties的文件,该文件与我们的域模型位于同一包中,并带有以下条目(请参阅:将EclipseLink MOXy指定为JAXB Provider )。

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

WSDL

以下是与我们的Web服务相对应的WSDL。 使用Provider方法的一个缺点是 JAX-WS实现无法自动为我们生成一个(请参阅: GlassFish 3.1.2充满了MOXy(EclipseLink JAXB) )。 WSDL是必需的,因为它为客户端定义了合同。 它甚至可以用于生成客户端。

<?xml version='1.0' encoding='UTF-8'?>
<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='FindCustomerService.xsd'/></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://localhost:8080/Blog-JAXWS/FindCustomerService'/></port></service>
</definitions>

XML模式

以下是与我们的消息有效负载相对应的XML模式。 使用Provider方法的一个缺点是JAX-WS实现不能直接利用JAXB直接直接自动生成XML模式,因此我们需要提供一个。

<?xml version='1.0' encoding='UTF-8'?>
<xsd:schema xmlns:ns0='http://service.jaxws.blog/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'targetNamespace='http://service.jaxws.blog/'><xsd:element name='findCustomerResponse' type='ns0:findCustomerResponse' /><xsd:complexType name='findCustomerResponse'><xsd:sequence><xsd:element name='return' type='ns0:customer'minOccurs='0' /></xsd:sequence></xsd:complexType><xsd:element name='findCustomer' type='ns0:findCustomer' /><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:schema>

请求对象

下面XML消息中突出显示的部分是我们将在Provider中作为Source实例接收的内容。 我们将创建一个JAXB模型来映射到此部分。

<?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>

FindCustomerRequest

根元素与主体的其余部分位于不同的XML名称空间中。 我们将利用@XmlRootElement批注指定名称空间(请参阅: JAXB&Namespaces )。

package blog.jaxws.provider;import javax.xml.bind.annotation.*;@XmlRootElement(namespace='http://service.jaxws.blog/', name='findCustomer')
public class FindCustomerRequest {private int arg0;public int getArg0() {return arg0;}public void setArg0(int arg0) {this.arg0 = arg0;}}

响应对象

下面XML消息中突出显示的部分是作为Source实例,我们将从Provider返回的内容。 我们将创建一个JAXB模型来映射到此部分。

<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'><S:Header /><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>

FindCustomerResponse

package blog.jaxws.provider;import javax.xml.bind.annotation.*;@XmlRootElement(namespace='http://service.jaxws.blog/')
public class FindCustomerResponse {private Customer value;@XmlElement(name='return')public Customer getValue() {return value;}public void setValue(Customer value) {this.value = value;}}

顾客

使用MOXy的众多原因之一是其基于路径的映射(请参阅: 基于XPath的映射 )。 以下是使用@XmlPath批注指定方式的示例

package blog.jaxws.provider;import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;@XmlType(propOrder = { 'firstName', 'lastName' })
public class Customer {private int id;private String firstName;private String lastName;@XmlAttributepublic int getId() {return id;}public void setId(int id) {this.id = id;}@XmlPath('personal-info/first-name/text()')public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}@XmlPath('personal-info/last-name/text()')public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}}

参考: Java XML和JSON绑定博客中的JCG合作伙伴 Blaise Doughan 通过JAX-WS Provider在Web服务中利用MOXy 。

翻译自: https://www.javacodegeeks.com/2013/02/leveraging-moxy-in-your-web-service-via-jax-ws-provider.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/369264.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python如何获取url中的内容_python怎么提取url中的参数

目标&#xff1a;结果&#xff1a;{tma_jssdk_version: 1.93.0.1, ac: WIFI, appTheme: dark}代码&#xff1a;import urllib.parse as urlparseurl https://search5-search-hl.amemv.com/aweme/v1/challenge/search/?tma_jssdk_version1.93.0.1&acWIFI&appThemedark…

子元素的margin-top会影响父元素

---恢复内容开始--- 之前在写项目的时候&#xff0c;发现原本想让父子元素之间加点边距&#xff0c;却让父元素产生了margin-top&#xff0c;于是百度之后发现了原因。 在css2.1盒模型中 In this specification, the expression collapsing margins means that adjoining ma…

spring boot高性能实现二维码扫码登录(上)——单服务器版

前言 目前网页的主流登录方式是通过手机扫码二维码登录。我看了网上很多关于扫码登录博客后&#xff0c;发现基本思路大致是&#xff1a;打开网页&#xff0c;生成uuid&#xff0c;然后长连接请求后端并等待登录认证相应结果&#xff0c;而后端每个几百毫秒会循环查询数据库或r…

查看 固态硬盘位置_3米防摔+人脸/指纹解锁:西数Armorlock移动固态硬盘

要求快速而又安全的数据拷贝工具&#xff1f;指纹识别移动SSD大家应该都见过了&#xff0c;今天西数推出了一个更为特别的人脸/指纹识别加密移动SSD。G-Technology Armorlock使用AES256全盘加密固态硬盘中的数据&#xff0c;解锁方式不是常见的密码或自带指纹传感器&#xff0c…

CCF - 201403-2 - 窗口

问题描述 试题编号&#xff1a;201403-2试题名称&#xff1a;窗口时间限制&#xff1a;1.0s内存限制&#xff1a;256.0MB问题描述&#xff1a; 问题描述在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域。窗口的边界上的点也属于该窗口。窗口之…

通过Spring集成从Hibernate 3迁移到4

本周是时候将我们的代码库升级到最新的Hibernate 4.x了。 我们推迟了迁移&#xff08;仍在Hibernate 3.3上&#xff09;&#xff0c;因为3.x分支的较新维护版本需要对API进行一些更改&#xff0c;这些更改显然仍在不断变化中。 一个示例是UserType API&#xff0c;该API仍然存在…

web前端工程师全套教程免费分享

这是我自己早前听课时整理的前端全套知识点&#xff0c;适用于初学者&#xff0c;也可以适用于中级的程序员&#xff0c;你们可以下载下来。我自认为还是比较系统全面的&#xff0c;可以抵得上市场上90%的学习资料。讨厌那些随便乱写的资料还有拿出来卖钱的人&#xff01;在这里…

vb在服务器上新建文件夹,vb.net-如果不存在,如何在VB中创建文件夹?

vb.net-如果不存在&#xff0c;如何在VB中创建文件夹&#xff1f;我为自己编写了一个小小的下载应用程序&#xff0c;以便我可以轻松地从服务器上获取一组文件&#xff0c;然后将它们全部放入带有全新安装的Windows的新PC上&#xff0c;而无需实际运行网络。 不幸的是&#xff…

mybatis一对一联表查询的两种常见方式

1.一条语句执行查询&#xff08;代码如下图&#xff09; 注释&#xff1a;class表&#xff08;c别名&#xff09;&#xff0c;teacher表&#xff08;t别名&#xff09;teacher_id为class表的字段t_id为teacher表的字段&#xff0c;因为两者有主键关联的原因&#xff0c;c_id为c…

在Windows 7中设置Java开发环境

一段时间以来&#xff0c;我收到了很多愿意尝试Java语言的学生和人们的要求&#xff0c;它们提供了关于如何设置Java开发环境的简单指南&#xff0c;类似于我一年前写的那样。 Mac用户。 看到这里和这里 。 因此&#xff0c;本文主要针对Java开发新手&#xff0c;他们寻求有关使…

写给想成为前端工程师的同学们―前端工程师是做什么的?

前端工程师是做什么的&#xff1f; 前端工程师是互联网时代软件产品研发中不可缺少的一种专业研发角色。从狭义上讲&#xff0c;前端工程师使用 HTML、CSS、JavaScript 等专业技能和工具将产品UI设计稿实现成网站产品&#xff0c;涵盖用户PC端、移动端网页&#xff0c;处理视觉…

逆水寒服务器维护7.5,逆水寒7.26日维护到什么时候 逆水寒7.26日游戏改动汇总介绍...

逆水寒7.26日维护到什么时候 逆水寒7.26日游戏改动汇总介绍2018-07-26 10:08:08来源&#xff1a;游戏下载编辑&#xff1a;苦力趴评论(0)《逆水寒》官方发布微博&#xff0c;称为了保证服务器的运行稳定和服务质量&#xff0c;将于7月26日上午7:00-上午10:00进行停服维护。此次…

是否可以限制蓝牙传输距离_技术文章—关于蓝牙传输范围的常见误解

蓝牙技术在耳机、手机、手表及汽车领域的普及为人们带来了许多便利&#xff0c;却也引发了一些人们对于蓝牙的误解。目前&#xff0c;蓝牙可为多种重要的解决方案提供支持&#xff0c;其中包括家庭自动化、室内导航以及商业和工业创新等。误解一&#xff1a;蓝牙稳定传输的最远…

shell 统计行数

语法&#xff1a;wc [选项] 文件… 说明&#xff1a;该命令统计给定文件中的字节数、字数、行数。如果没有给出文件名&#xff0c;则从标准输入读取。wc同时也给出所有指定文件的总统计数。字是由空格字符区分开的最大字符串。 该命令各选项含义如下&#xff1a; - c 统计字节数…

Async分析

1&#xff1a;android在新版本中不允许UI线程访问网络&#xff0c;但是如果需要访问网络又改怎么办呐&#xff1f;这里有很多解决方案&#xff0c;比如新开一个线程&#xff0c;在新线程中进行访问&#xff0c;然后访问数据&#xff0c;返回后可能会更新界面也可能不更新界面&a…

JavaFX即将推出您附近的Android或iOS设备吗?

已经有大新闻最近在世界上的JavaFX的关于JavaFX的是许多更多的组件开源&#xff0c;开源的广告在2012 JavaOne大会 。 在2月的开源更新中 &#xff0c; Richard Bair汇编了一份JavaFX项目表&#xff0c;该表在撰写本文时&#xff08;2013年2月11日&#xff0c;星期一&#xff0…

基于webpack搭建的vue element-ui框架

花了1天多的时间&#xff0c; 终于把这个框架搭建起来了。 好了&#xff0c; 不多说了&#xff0c; 直接进入主题了。前提是安装了nodejs,至于怎么安装&#xff0c; 网上都有教程。 这里就不多说了&#xff0c; 这边使用的IDE是idea。1.在E:/my-project&#xff08;我的电脑上&…

mvc怎么请求服务器错误信息,asp.net-mvc – IIS显示服务器错误而不是自定义错误...

我正在使用MVC 5,我正在使用自定义视图处理我的错误,例如(404,403 ……等)它在我的本地IIS上工作正常,但是当我在登台服务器上发布时,它显示有关这些错误代码的IIS服务器错误消息.它显示了这条消息&#xff1a;代替&#xff1a;我修改了web.config for< customErrors mode “…

编译打包vue_Vue 源码分析( 一 )

Vue 源码分析&#xff08; 一 &#xff09;目录结构、版本、入口1、Vue 源码目录结构dist&#xff1a;打包之后生成的结果目录 examples&#xff1a;代码示例 scripts&#xff1a;配置文件 src&#xff1a;源代码目录compiler: 编译相关 &#xff08;将template模板转换成rende…

List 集合转换 json 字符串 ajax前台拼接

List 集合 转换为json 字符串public object Taoshow(){var i pbll.PackShow();//list集合var lida JsonConvert.SerializeObject(i); //转换成json字符串return lida;}function Tao() {$.ajax({url: "/Wangjie/Taoshow",type: "Get",dataType: "Jso…