DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Implementing a JAX-WS Web Service Accessing its Input as XML Source

Implementing a JAX-WS Web Service Accessing its Input as XML Source

Jakub Holý user avatar by
Jakub Holý
·
Jan. 01, 11 · Interview
Like (0)
Save
Tweet
Share
29.81K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes you may want to create a JAX-WS webservice with its input defined by a proper, structured XSD yet accessing the input as raw XML object and not as POJOs produced by JAXB, similarly as with a JAX-RPC webservice having input of the type SOAPElement. This is possible using @WebServiceProvider with javax.xml.ws.Service.Mode.PAYLOAD.

JAXB input Normally you create a JAX-WS webservice by annotating a POJO with @WebService and related annotations and use JAXB annotations on your domain classes used in input and output of the web service (or you use wsimport and the JAXB compiler to produce these from a WSDL file).

XML input But there is also an alternative way for accessing the input as XML data (not just a string containing XML!), in which case you basically bypass JAXB binding and thus you also don’t need to generate any domain classes for your wsdl.

JAX-WS 2.0 specification, section 5.1 javax.xml.ws.Provider (page 77) explains the difference:

Java SEIs [JH: a native Java service endpoint interface] provide a high level Java-centric abstraction that hides the details of converting between Java objects and their XML representations for use in XML-based messages. However, in some cases it is desirable for services to be able to operate at the XML message level. The Provider interface offers an alternative to SEIs and may be implemented by services wishing to work at the XML message level.

Implementation example

This is how you would create such a web service, accessing its input as XML data represented by javax.xml.transform.Source:

ExampleRawXmlServiceImpl.java:

package example;

import javax.xml.transform.Source;
import javax.xml.ws.Provider;

@javax.xml.ws.ServiceMode(value=javax.xml.ws.Service.Mode.PAYLOAD)
@javax.xml.ws.WebServiceProvider(wsdlLocation="WEB-INF/wsdl/learningActivity/LearningActivity.wsdl"
, targetNamespace="http://w3.ibm.com/xmlns/ibmww/hr/learning/lms/br/la"
, serviceName="LearningActivityHttpService"
, portName="LearningActivityRawXml")
public class ExampleRawXmlServiceImpl implements Provider<Source> {
@Override
public Source invoke(final Source request) {
// ...
return null;
}
}

(You perhaps don’t need all the @WebServiceProvider attributes.)

The important things to notice:

  • @WebServiceProvider is used instead of @WebService
  • The ServiceMode is set to Payload and therefore the class has to implement Provider<javax.xml.transform.Source>. You could set it also to Message, in which case you’d get a complete SOAPMessage and thus you’d need to implement Provider<SOAPMessage>.

Working with the XML data (Source)

You will likely need to convert the input javax.xml.transform.Source to something usable. Here are few examples:
Converting Source to XML string:

import java.io.StringWriter;
import import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
...
final StringWriter requestXmlWriter = new StringWriter();
final Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.transform(request, new StreamResult(requestXmlWriter));
final String requestXml = requestXmlWriter.toString();

Converting Source to DOM (copied from [1]):

import javax.xml.transform.dom.*;
import org.w3c.dom.Node;
...
DOMResult dom = new DOMResult();
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.transform(source, dom);
Node node = dom.getNode();
// do something with it ...
DOMSource src = new DOMSource (node);

Notice there is also javax.xml.transform.sax containing SAXResult and SAXSource.

Resources

  1. Article Realizing Strategies for Document-Based Web Services With JAX-WS 2.0: Part 3 in a Series by Sameer Tyagi, 2005 – section Switching Off Data Binding
  2. Blog Operating at the XML Message Level in JAX-WS 2.0 by Art Frechette, 2006
  3. JAX-WS 2.0 specification (JSR 224)

From http://theholyjava.wordpress.com/2010/12/29/implementing-jax-ws-webservice-accessing-its-input-as-xml-source-similar-to-jax-rpc-with-soapelement-input/

XML Web Service

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Master Spring Boot 3 With GraalVM Native Image
  • Reconciling Java and DevOps with JeKa
  • 10 Most Popular Frameworks for Building RESTful APIs
  • Isolating Noisy Neighbors in Distributed Systems: The Power of Shuffle-Sharding

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: