在先前的文章中,我演示了如何将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