Giter VIP home page Giter VIP logo

gs-consuming-web-service's Introduction

tags projects
SOAP
spring-ws

This guide walks you through the process of consuming a SOAP-based web service with Spring.

What you’ll build

You will build a client that fetches weather data from a remote, WSDL-based web service using SOAP. You can find out more about the quote service at http://www.webservicex.com/stockquote.asmx?op=GetQuote.

The service provides stock market quotes. You will be able to use your own ticker symbol.

What you’ll need

Note
If you read Producing a SOAP web service, you might be wondering why this guide doesn’t use spring-boot-starter-ws? That Spring Boot starter is only for server-side web services. That starter brings on board things like embedded Tomcat, which isn’t need to make a web call.

Generate domain objects based on a WSDL

The interface to a SOAP web service is captured in a WSDL. JAXB provides an easy means to generate Java classes from a WSDL (or rather: the XSD contained in the <Types/> section of the WSDL). The WSDL for the quote service can be found at http://www.webservicex.com/stockquote.asmx?WSDL.

To generate Java classes from the WSDL in maven, you need the following plugin setup:

link:complete/pom.xml[role=include]

This setup will generate classes for the WSDL found at the specified URL, putting those classes in the hello.wsdl package.

To do the same with gradle, you will need the following in your build file:

link:complete/build.gradle[role=include]

As gradle does not have a JAXB plugin (yet), it involves an ant task, which makes it a bit more complex than in maven.

In both cases, the JAXB domain object generation process has been wired into the build tool’s lifecycle so there are no extra steps to run.

Create a weather service client

To create a web service client, you simply have to extend the WebServiceGatewaySupport class and code your operations:

src/main/java/hello/QuoteClient.java

link:complete/src/main/java/hello/QuoteClient.java[role=include]

The client contains one method: getQuote which does the actual SOAP exchange.

In this method, both the GetQuote and the GetQuoteResponse classes are derived from the WSDL and were generated in the JAXB generation process described in the previous step. It creates the GetQuote request object and sets it up with the ticker parameter. After printing out the ticker code, it uses the WebServiceTemplate supplied by the WebServiceGatewaySupport base class to do the actual SOAP exchange. It passes the GetQuote request object, as well as a SoapActionCallback to pass on a SOAPAction header with the request, as the WSDL described that it needed this header in the <soap:operation/> elements. It casts the response into a GetQuoteResponse object, which is then returned.

Configuring web service components

Spring WS uses Spring Framework’s OXM module which has the Jaxb2Marshaller to serialize and deserialize XML requests.

src/main/java/hello/QuoteConfiguration.java

link:complete/src/main/java/hello/QuoteConfiguration.java[role=include]

The marshaller is pointed at the collection of generated domain objects and will use them to both serialize and deserialize between XML and POJOs.

The quoteClient is created and configured with the URI of the weather service shown up above. It is also configured to use the JAXB marshaller.

Make the application executable

This application is packaged up to run from the console and retrieve a single weather forecast for a given zip code.

src/main/java/hello/Application.java

link:complete/src/main/java/hello/Application.java[role=include]

The main() method defers to the SpringApplication helper class, providing QuoteConfiguration.class as an argument to its run() method. This tells Spring to read the annotation metadata from QuoteConfiguration and to manage it as a component in the Spring application context.

Note
This application is hard coded to look up zip code 94304, Palo Alto, CA. Towards the end of this guide, you’ll see how to plug in a different zip code without editing the code.

Logging output is displayed. The service should be up and running within a few seconds.

Requesting quote for MSFT

<StockQuotes><Stock><Symbol>MSFT</Symbol><Last>62.70</Last>...</StockQuotes>

You can plug in a different ticker by typing java -jar build/libs/gs-consuming-web-service-0.1.0.jar ORCL

Requesting quote for ORCL

<StockQuotes><Stock><Symbol>ORCL</Symbol><Last>39.26</Last>...</StockQuotes>

Summary

Congratulations! You’ve just developed a client to consume a SOAP-based web service with Spring.

gs-consuming-web-service's People

Contributors

gregturn avatar huangbowen521 avatar kcbaltz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.