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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How To Get Cell Data From an Excel Spreadsheet Using APIs in Java
  • 8 Strategies To Accelerate Web Portal Development
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)
  • How To Create a Restful Web Service Using Low Code Integration Platform

Trending

  • Proactive Security in Distributed Systems: A Developer’s Approach
  • My Favorite Interview Question
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Using JAXB and JAX-WS for service with custom headers

By 
Pieter van der Meer user avatar
Pieter van der Meer
·
Sep. 08, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
23.7K Views

Join the DZone community and get the full member experience.

Join For Free
Since the introduction of the JAX-WS in Java EE building SOAP web-services have never been easier (as compared to i.e. Spring WS). Just a few annotations and you are up and running. Disadvantage of this all is that it always goes from a Contract first or Java first approach. While in a lot of environments this is the case, situations are there when its a mixed case.

Just recently I ran into the mixed case. Needed to replace an existing service that did not have any WSDL’s but the XSD’s for the payload was there. The following are the steps I executed to implement the new service from the existing XSD’s and included inbound and outbound headers.

Generating the ObjectFactories from the supplied XSD’s is simple in maven just add the following snippet to the your maven pom file:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>1.6</version>
  <executions>
    <execution>
      <id>xjc</id>
      <goals>
        <goal>xjc</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <bindingFiles>
      bindings.xjb
    </bindingFiles>
  </configuration>
</plugin>

That is all to have the JAXB files generated. The bindings file I specified was allows you to customize the generated classes. I tend to convert the xsd:DateTime to JodaTime, but that is just me.

Next the definition of the JAX-WS services. On the Internet there are a lot of guides available to get started with JAX-WS. No point in getting into that. I found that information on dealing with headers is mixed.

  • https://metro.java.net/nonav/1.2/guide/SOAP_headers.html
    Solution depends on the com.sun.xml.ws.developer classes. Not where I want to go.
  • http://www-01.ibm.com/support/knowledgecenter/SS7K4U_8.5.5/com.ibm.websphere.nd.multiplatform.doc/ae/twbs_cookiejaxws.html
    Suggests a solution where the headers are set separately. Does not even answer how to do  inbound headers.
  • http://stackoverflow.com/questions/830691/how-do-i-add-a-soap-header-using-java-jax-ws
    Just a sample of the answers that are available on stackoverflow. The solution provided here is the most common.

All the solutions provided will do the trick to get headers in the message. What you do not find a lot is a solution that uses the @WebParam annotation in the argument list of the web service. The annotation has a two important fields for header support; header and mode.

Setting  the header field to true, will add this to the header part of the soap message. the mode flag allows the following values:

  • IN
    Inbound
  • OUT
    Outbound
  • INOUT
    In and Outbound.

The first two are easy to use. the last, INOUT has a bit more to it.

When using the WebParam.Mode.INOUT value changes are high that the container you are running is not able to generate the WSDL and XSD files. The error you get is ambiguous. End result your WSDL is not generated hence the service is not available.  The key when using the INOUT mode is the generic class javax.xml.ws.Holder<T>. When the parameter is placed inside this type. The WSDL is generated correctly.

Resulting @WebMethod method:

 @WebMethod(
        action = "ServiceName",
        operationName = "operation"
    )
    public MyResponse operation(
        @WebParam(mode = WebParam.Mode.INOUT, header = true, name = "JaxbHeader",
                  targetNamespace = "http://www.elucidator.nl/header/v1.0")
        final Holder headerHolder,
        @WebParam(mode = WebParam.Mode.IN, name = "data") final JaxbPayload payload)
        throws MyServiceException
  {
    //SNIP
    if (headerHolder.value.flag) {
       headerHolder.value.field = "SomeValue";
    return responseObject;
  }

Access to the values in the header object is straight forward. when you modify the values the response contains the modified values. With this solution there is no need to write custom  SOAPHandler implementations for headers.

The generated WSDL will be:

<definitions xmlns:ns1="..." xmlns:tns="..." 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
     xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" 
     name="MyService" 
     targetNamespace="..." 
     xmlns="http://schemas.xmlsoap.org/wsdl/">
    <types>
       //SNIP
    </types>
    <message name="operation">
        <part name="parameters" element="tns:operation"/>
        <part name="jaxbHeader" element="ns1:JaxbHeader"/>
    </message>
    <message name="operationResponse">
        <part name="result" element="tns:operationResponse"/>
        <part name="JaxbHeader" element="ns1:JaxbHeader"/>
    </message>
    <portType name="ServiceName">
        <operation name="insert" parameterOrder="parameters JaxbHeader">
            <input message="tns:operation" wsam:Action="operation"/>
            <output message="tns:operationResponse" wsam:Action="http://www.elucidator.nl/ws/v1.0/operationResponse"/>
        </operation>
    </portType>
    <binding name="ServiceNameBinding" type="tns:ServiceName">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="insert">
            <soap:operation soapAction="operation"/>
            <input>
                <soap:body parts="parameters" use="literal"/>
                <soap:header message="tns:operation" part="JaxbHeader" use="literal"/>
            </input>
            <output>
                <soap:body parts="result" use="literal"/>
                <soap:header message="tns:operationResponse" part="JaxbHeader" use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="ServiceName">
        <port name="ServiceNamePort" binding="tns:ServicePortBinding">
            <soap:address location="http://localhost:9081/Service"/>
        </port>
    </service>
</definitions>

As you can see a nicely generated WSDL that contains information about the headers that are expected and returned.

There is however 1 minor disadvantage with this solution. When an exception is thrown you do not have access to the headers anymore.  I have a solution for this, implementation of a Handler that intercepts the request, stores the header and modifies the fault response in the handleFault method. But that is a subject for an other post.

 

Original can be found on: Elucidator 

Web Service

Opinions expressed by DZone contributors are their own.

Related

  • How To Get Cell Data From an Excel Spreadsheet Using APIs in Java
  • 8 Strategies To Accelerate Web Portal Development
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)
  • How To Create a Restful Web Service Using Low Code Integration Platform

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!