Soap message example in java

Invoking a SOAP Web Service in Java

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We rely on other people’s code in our own work. Every day.

It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.

Lightrun is a new kind of debugger.

It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.

Learn more in this quick, 5-minute Lightrun tutorial:

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

Читайте также:  Css button style with icon

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We’re looking for a new Java technical editor to help review new articles for the site.

1. Overview

In this tutorial, we’ll learn how to build a SOAP client in Java with JAX-WS RI in Java 8 and 11.

First, we’ll generate the client code using the wsimport utility and then test it using a JUnit.

For those starting out, our introduction to JAX-WS provides great background on the subject.

2. The Web Service

Before we start building a client, we need a server. In this case, we need a server exposing a JAX-WS web service.

For the purpose of this tutorial, we’ll use a web service that will fetch us a country’s data, given its name.

2.1. Summary of Implementation

Since we’re focusing on building the client, we won’t get into the implementation details of our service.

Let’s say that an interface CountryService is used to expose the web service to the external world. To keep things simple, we’ll build and deploy the web service using the javax.xml.ws.Endpoint API in our class CountryServicePublisher.

We’ll run CountryServicePublisher as a Java application to publish an endpoint that’ll accept the incoming requests. In other words, this will be our server.

After starting the server, hitting the URL http://localhost:8888/ws/country?wsdl gives us the web service description file. The WSDL acts as a guide to understand the service’s offerings and generate implementation code for the client.

2.2. The Web Services Description Language

Let’s look at our web service’s WSDL, country:

  targetNamespace="http://server.ws.soap.baeldung.com/" name="CountryServiceImplService">                      

In a nutshell, this is the useful information it provides:

  • We can invoke the method findByName with a string argument.
  • In response, the service will return us a custom type of country.
  • Types are defined in an xsd schema generated at the location http://localhost:8888/ws/country?xsd=1:
  targetNamespace="http://server.ws.soap.baeldung.com/"> 

That’s all we need to implement a client.

Let’s see how in the next section.

3. Using wsimport to Generate Client Code

3.1. For JDK 8

First, let’s see how to generate client code using JDK 8.

To begin with, let’s add a plugin to our pom.xml to use this tool via Maven:

 org.codehaus.mojo jaxws-maven-plugin 2.6  wsimport-from-jdk wsimport     http://localhost:8888/ws/country?wsdl  true com.baeldung.soap.ws.client.generated src/main/java  

Second, let’s execute this plugin:

Читайте также:  Dump mysql php script

That’s all! The above command will generate code in the specified package com.baeldung.soap.ws.client.generated inside the sourceDestDir we provided in the plugin configuration.

Another way to achieve the same would be to use the wsimport utility. It comes out of the box with the standard JDK 8 distribution and can be found under JAVA_HOME/bin directory.

To generate client code using wsimport, we can navigate to the project’s root and run this command:

JAVA_HOME/bin/wsimport -s src/main/java/ -keep -p com.baeldung.soap.ws.client.generated "http://localhost:8888/ws/country?wsdl"

It’s important to bear in mind that the service endpoint should be available in order to successfully execute the plugin or command.

3.2. For JDK 11

Starting JDK 11, wsimport was removed as part of the JDK and no longer comes out of the box with the standard distribution.

However, it was open-sourced to the Eclipse foundation.

In order to use wsimport to generate client code for Java 11 and above, we need to add the jakarta.xml.ws-api, jaxws-rt and jaxws-ri dependencies in addition to the jaxws-maven-plugin:

  jakarta.xml.wsjakarta.xml.ws-api3.0.0  com.sun.xml.ws jaxws-rt 3.0.0runtime  com.sun.xml.ws jaxws-ri 2.3.1pom     com.sun.xml.ws jaxws-maven-plugin 2.3.2  http://localhost:8888/ws/country?wsdl  true com.baeldung.soap.ws.client.generated src/main/java     

Now, to generate the client code in the package com.baeldung.soap.ws.client.generated, we’ll need the same Maven command as before:

Next, let’s look at the generated artifacts that are the same for both the Java versions.

3.3. Generated POJOs

Based on the xsd we saw earlier, the tool will generate a file named Country.java:

@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "country", propOrder = < "capital", "currency", "name", "population" >) public class Country < protected String capital; @XmlSchemaType(name = "string") protected Currency currency; protected String name; protected int population; // standard getters and setters >

As we can see, the generated class is decorated with JAXB annotations for marshalling and unmarshalling the object to and from XML.

Also, it generates a Currency enum:

@XmlType(name = "currency") @XmlEnum public enum Currency < EUR, INR, USD; public String value() < return name(); >public static Currency fromValue(String v) < return valueOf(v); >>

3.4. CountryService

The second generated artifact is an interface that acts as a proxy to the actual web service.

The interface CountryService declares the same method as our server, findByName:

@WebService(name = "CountryService", targetNamespace = "http://server.ws.soap.baeldung.com/") @SOAPBinding(style = SOAPBinding.Style.RPC) @XmlSeeAlso(< ObjectFactory.class >) public interface CountryService < @WebMethod @WebResult(partName = "return") @Action(input = "http://server.ws.soap.baeldung.com/CountryService/findByNameRequest", output = "http://server.ws.soap.baeldung.com/CountryService/findByNameResponse") public Country findByName(@WebParam(name = "arg0", partName = "arg0") String arg0); >

Notably, the interface is marked as a javax.jws.WebService, with a SOAPBinding.Style as RPC as defined by the service’s WSDL.

The method findByName is annotated to declare that it’s a javax.jws.WebMethod, with its expected input and output parameter types.

3.5. CountryServiceImplService

Our next generated class, CountryServiceImplService, extends javax.xml.ws.Service.

Its annotation WebServiceClient denotes that it is the client view of a service:

@WebServiceClient(name = "CountryServiceImplService", targetNamespace = "http://server.ws.soap.baeldung.com/", wsdlLocation = "http://localhost:8888/ws/country?wsdl") public class CountryServiceImplService extends Service < private final static URL COUNTRYSERVICEIMPLSERVICE_WSDL_LOCATION; private final static WebServiceException COUNTRYSERVICEIMPLSERVICE_EXCEPTION; private final static QName COUNTRYSERVICEIMPLSERVICE_QNAME = new QName("http://server.ws.soap.baeldung.com/", "CountryServiceImplService"); static < URL url = null; WebServiceException e = null; try < url = new URL("http://localhost:8888/ws/country?wsdl"); >catch (MalformedURLException ex) < e = new WebServiceException(ex); >COUNTRYSERVICEIMPLSERVICE_WSDL_LOCATION = url; COUNTRYSERVICEIMPLSERVICE_EXCEPTION = e; > public CountryServiceImplService() < super(__getWsdlLocation(), COUNTRYSERVICEIMPLSERVICE_QNAME); >// other constructors @WebEndpoint(name = "CountryServiceImplPort") public CountryService getCountryServiceImplPort() < return super.getPort(new QName("http://server.ws.soap.baeldung.com/", "CountryServiceImplPort"), CountryService.class); >private static URL __getWsdlLocation() < if (COUNTRYSERVICEIMPLSERVICE_EXCEPTION != null) < throw COUNTRYSERVICEIMPLSERVICE_EXCEPTION; >return COUNTRYSERVICEIMPLSERVICE_WSDL_LOCATION; > >

The important method to note here is getCountryServiceImplPort. Given a qualified name of the service endpoint, or QName, and the dynamic proxy’s service endpoint interface name, it returns a proxy instance.

To invoke the web service, we need to use this proxy, as we’ll see shortly.

Using a proxy makes it seem as if we are calling a service locally, abstracting away the intricacies of remote invocation.

4. Testing the Client

Next, we’ll write a JUnit test to connect to the web service using the generated client code.

Before we can do that, we need to get the service’s proxy instance at the client end:

@BeforeClass public static void setup()

For more advanced scenarios such as enabling or disabling a WebServiceFeature, we can use other generated constructors for CountryServiceImplService.

Now let’s look at some tests:

@Test public void givenCountryService_whenCountryIndia_thenCapitalIsNewDelhi() < assertEquals("New Delhi", countryService.findByName("India").getCapital()); >@Test public void givenCountryService_whenCountryFrance_thenPopulationCorrect() < assertEquals(66710000, countryService.findByName("France").getPopulation()); >@Test public void givenCountryService_whenCountryUSA_thenCurrencyUSD()

As we can see, invoking the remote service’s methods became as simple as calling methods locally. The proxy’s findByName method returned a Country instance matching the name we provided. Then we used various getters of the POJO to assert expected values.

5. Conclusion

In this article, we saw how to invoke a SOAP web service in Java using JAX-WS RI and the wsimport utility for Java 8 as well as 11.

Alternatively, we can use other JAX-WS implementations such as Apache CXF, Apache Axis2 and Spring to do the same.

As always, the source code is available over on GitHub for both JDK 8 and JDK 11 versions.

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

Источник

Оцените статью