Handle uncaught exceptions in a Spring-Boot REST API – Part VI

Many times, we have seen exception thrown on your web page and all the stack trace of the exception. A non-technical user will not be able to understand most of the time. Also stack trace is not the best option when we can show the same exception error in nice json format with the root cause. In this post, we will show how to handle unhandled exceptions from our previous Spring REST API we built here. This particular feature will show how to handle most HTTP 500 errors which happen because of server side issues. Any errors with request or client side, those are HTTP 400 errors and they have been handled in previous post Error Handling in Spring Boot Rest API.

Problem

What happens when there is a database connection issue OR columns in your REST API are different from specified in database tables? Your API will throw a 500 error and if you don’t have any mechanism, this will display error in an html format which will not give much information to user or developer to resolve the issue.

Solution

JAX-RS provides a way to handle uncaught exceptions. This can be done by my implementing an interface for ExtendedExceptionMapper. Basically this is a contract for a provider that takes java exceptions and maps them to a response object which can be transformed into a JSON. This can be implemented as below:


package com.betterjavacode.benefits.utilities;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.jersey.spi.ExtendedExceptionMapper;

@Provider
public class UncaughtExceptionMapper implements ExtendedExceptionMapper<Throwable> {

private static final Logger LOGGER = LogManager.getLogger(UncaughtExceptionMapper.class);

@Override
public Response toResponse(Throwable exception) {
LOGGER.info("Enter >> toResponse ");
LOGGER.debug("Exception Caught: " + exception.getMessage());
LOGGER.info("Exit << toResponse");
return Response.status(Status.BAD_REQUEST)
.entity(exception.getMessage())
.build();
}

@Override
public boolean isMappable(Throwable arg0) {
return !(arg0 instanceof WebApplicationException);
}

}

Basically this implementation shows a class UncaughtExceptionMapper implementing an interface ExtendedExceptionMapper which provides a way to map all exceptions which are not of type WebApplicationException (Most HTTP 400 errors are WebApplicationExceptions). toResponse method will help to log all the exceptions and convert exceptions into a Response object.

Conclusion

In this post, we showed how to map all uncaught exceptions into json format response. The code for this post is available at github repository.

How to compare Strings in Java and more about Strings

Yes, I will be talking about one of the favorite topics or may be the most argued topic of strings in programming languages. This post will not cover any other programming languages, but Java.

Question

One of the beginner question in Java is how to compare Strings in java?

Solution

Short answer is .equals compare the values of two strings and see if they are equal or not. Operator == compare the two string objects and see if they are referring to same memory address.

Example:

String str1 = new String(“test”);

String str2 = new String(“test”);

str1.equals(str2) – This will return true.

str1 == str2 – This will return false.

Comment

Despite Java supports operator == for strings, it is not used for string comparison often. Object reference check happens rarely. Another anomaly to this is if two strings are null.

Example:

String str1 = null;

String str2 = null;

str1 == str2 – this will return true.

str1.equals(str2) – this will throw null pointer exception (NPE).

Java offers another method called compareTo for String comparison. This can be used in following way

Example:

String str1 = “test”;

String str2 = “test”;

str1.compareTo(str2) == 0 – This will return true.

More about Strings

We all have seen StringBuffer, StringBuilder, StringWriter, StringTokenizer or just plain String. What are all these different aspects of String and when to use? Isn’t this too much to know when you just want to use a simple String object. This post will cover some information about all these different classes that Java offers.

StringBuffer

StringBuffers are thread-safe, they are just synchronized version of StringBuilder. StringBuffer offers some helpful methods like append and reverse.

Example:

StringBuffer sb = new StringBuffer();

sb.append(“first string”);

sb.toString();

StringBuilder

StringBuilder are not thread-safe, but they offer better performance than StringBuffer. You can say StringBuffer are thread-safe version of StringBuilder.

StringTokenizer

StringTokenizer is completely different from StringBuffer and StringBuilder as it is mainly used for splitting strings into token at a delimiter. StringBuffer and StringBuilder are used to build strings.

StringWriter

StringWriter is a character stream that collects the output in a string buffer. In short, you can say it uses StringBuffer underneath. This is an IO stream, very similar to File IO, but you can still access StringWriter even after stream has been closed.

Example:

StringWriter sw = new StringWriter();

sw.write(“this is first string”);

sw.toString();

Conclusion

In this article, we showed String comparison and different forms of String building.

How to consume OAuth secured SOAP web service

I faced the issue where I had to consume a SOAP service which was secured by OAuth1.0a. And Spring doesn’t provide any direct solution for consumer OAuth secured SOAP service.

In Producing and Consuming SOAP web service and Consuming SOAP web service over HTTPS, we saw how to consume a SOAP web service. In this post, we will go little beyond this and implement a solution to consume OAuth secured SOAP web service. Securing a web service is general trend and you must secure a web service if you are letting others consume it. This is a secure way to transfer data between producer and consumer without compromising customer data.

Pre-requisites

  1. Spring web services
  2. OAuth library and knowledge

How to implement?

Below is a code that shows how to send a SOAP request call to a web service if it is not OAuth secured.

public class UserClient extends WebServiceGatewaySupport
{
    public GetUserResponse getUserById (int userid)
    {
       GetUserRequest userrequest = new GetUserRequest();
       userrequest.setId(userid);
       GetUserResponse response = (GetUserResponse)getWebServiceTemplate().marshalSendAndReceive(userrequest, new SoapActionCallback("https://localhost:8443/benefits/endpoints/getUserResponse"));
       return response;
     }
}

Basically, we are using a WebServiceTemplate to marshal a request and send it to SOAP endpoint. SoapActionCallback  is callback which allows to change the marshalled message before it can be sent to endpoint and a response can be retrieved.

As part of this solution, we will implement a class SignedMessageSender that will sign the request with OAuth consumer key and secret.


public class SignedMessageSender extends HttpComponentsMessageSender
{
    private final CommonsHttpOAuthConsumer consumer;

    public SignedMessageSender(CommonsHttpOAuthConsumer consumer)
    {
      this.consumer = consumer;
    }

    public WebServiceConnection createConnection(URI uri)
    {
       HttpComponentsConnection conn = null;
       try
       {
          conn = (HttpComponentsConnection)super.createConnection(uri);
consumer.sign(connection.getHttpPost());
       }
       catch (IOException e | OAuthException e)
       {
          e.printStackTrace();
       }
       return conn;
}

Now we build our bean for client to use this message sender, we will assign consumer key and consumer secret. This also uses JAXB marshaller. The code for this will look like below


@Bean

public UserClient getUserClient(Jaxb2Marshaller marshaller)

{

UserClient us = new UserClient();

us.setDefaultUri("https://localhost:8443/benefits/endpoints/users.wsdl");

us.setMarshaller(marshaller);

us.setUnmarshaller(marshaller);

String consumerkey = "";

String secretkey = "";

CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerkey,secretkey);

SignedMessageSender signedMessageSender = new SignedMessageSender(consumer);

signedMessageSender.createConnection(new URL("https://localhost:8443/benefits/endpoints/users.wsdl").toURI());

us.setMessageSender(signedMessageSender);

return us;

}

 

This shows how we can implement a solution to consume a SOAP web service secured by OAuth 1.0a. I am sure we can add similar solution if the service is secured by OAuth 2.0, but that will be another post.

Conclusion

In this post, we showed how to add OAuth signed SOAP message to SOAP service which would return a response to consumer.

References

  1. Add Header to SOAP message
  2. SOAP WS-addressing
  3. https://www.avisi.nl/blog/2012/11/22/consuming-oauth-secured-soap-webservices-using-spring-ws-axiom-signpost/

 

Guidelines to avoid null check statements

Have you ever seen a code like below?


Object obj = anotherobj.getObject();
if(obj != null)
{
&nbsp; &nbsp; // do something with obj
}

Imagine if you have to write such if-statement block for every object or variable you retrieve from different contract objects in your code, it would make the code cumbersome and unreadable. Also it gives an impression of naivety of developer in such cases.

I am writing this post based on a heavy discussion happening on stackoverflow Avoiding Null Statements. I am only using this post as a reference and writing my own guidelines based on my own experience. Answers to that post on stackoverflow are worth to checkout.

Guidelines to avoid checking null statements

  1. If callee keeps a contract of never returning a null, then caller doesn’t have to add any check statement for null value. But this is basic premise a developer should follow when writing methods.
  2. Most of the time, the issue is not method returning null, but the logic implemented in these methods is not accurate. If business logic of a method knows a way to handle errors in cases when it can return the right data, it should return an error instead of returning null.
 
public String getEmployeeInformation(String empcode) 
{ 
   if (empcode == null) 
   { 
      // instead of returning null, throw an error
      throw new IllegalArgumentException(" empcode is null "); 
   } 
} 

3.  Java 8 offers a new keyword optional 


public Optional<Employee> getEmployeeInfoWithSSN(String ssn)

{

..............

}

So if the employee information with particular SSN is not found, then caller of this method has to explicitly think about type system.

4.  Intellij Idea offers Java annotations like @Nullable and @NotNull

5.  Write junit test cases for your classes which check for assertNotNull()

6.  Java 7 offers a utility method for Objects , Objects.requireNonNull();

 

References –

  1. Avoid null check statements
  2. Optional keyword

Consuming a SOAP webservice over HTTPS

In the previous post, we talked about producing and consuming a SOAP web service here. This post will be a sequel to that post since recently I faced a similar issue during my project. In this post, we will talk about how to consume a SOAP web service over https. Since this will be a small post, we will not be posting any code on github.

Problem –

While consuming a SOAP web service which is behind SSL, if you don’t handle SSL certificates, you will run into following error


sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
 at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
 at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
 at sun.security.validator.Validator.validate(Validator.java:260)
 at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
 at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
 at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
 at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1351)
 at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:156)
 at sun.security.ssl.Handshaker.processLoop(Handshaker.java:925)
 at sun.security.ssl.Handshaker.process_record(Handshaker.java:860)
 at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1043)
 at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
 at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:728)
 at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
 at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:138)
 at SSLPoke.main(SSLPoke.java:31)
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
 at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145)
 at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
 at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
 at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
 ... 15 more

Solution –

Basically this error is happening if your SOAP web service is on SSL and the client is trying to connect to web service, web service doesn’t recognize the client and throws this error.

To resolve this error, you can download a SSL certificate from the server where you are hosting SOAP web service and import that certificate on your client machine’s keystore. In production environment, you should have a way to access this keystore when a call is made to the web service.

Let’s assume that our webservice from post is on SSL, like https://localhost:8943/benefits/endpoints/users.wsdl. If you access this URL in browser, you will be able to see the SSL certificate. Export this SSL certificate in base 64 format file, example sslcertificate.crt. Import this certificate in


keytool -import -alias sslcertificateofserver -keystore truststore.jks -storepass changeit -file sslcertificate.crt

Now, we will change the configuration class we wrote to configure web service components.


package com.betterjavacode.benefits.views;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

@Configuration
public class ClientAppConfig
{
   private Resource getKeyStore()
   {
      Environment.getProperty("betterjavacode.com.keystore");
   }

   private String getKeystorePassword()
   {
      Environment.getProperty("betterjavacode.com.keyStorePassword");
   }

   @Bean
   public Jaxb2Marshaller marshaller()
   {
     Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
     marshaller.setContextPath("com.betterjavacode.benefits.soap");
     return marshaller;
   }

   @Bean
    public UserClient userClient(Jaxb2Marshaller marshaller) {
        // WSDL URL - http://localhost:8080/benefits/endpoints/users.wsdl
        UserClient uc = new UserClient();
        uc.setDefaultUri("http://localhost:8080/benefits/endpoints/users.wsdl");
        uc.setMarshaller(marshaller);
        uc.setUnmarshaller(marshaller);

        FileInputStream fis = new FileInputStream(getKeyStore());
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(fis, getKeyStorePassword().toCharArray());

        try {
            fis.close();
        } catch (IOException e) {
        }
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(ks, keyStorePassword.toCharArray());

        FileInputStream fisTS = new FileInputStream(getKeyStore());
        KeyStore ts = KeyStore.getInstance("JKS");
        ts.load(fisTS, trustStorePassword.toCharArray());

        try {
            fisTS.close();
        } catch(IOException e) {
        }
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(ts);

        HttpsUrlConnectionMessageSender messageSender = new HttpsUrlConnectionMessageSender();
        messageSender.setKeyManagers(keyManagerFactory.getKeyManagers());
        messageSender.setTrustManagers(trustManagerFactory.getTrustManagers());

        HostNameVerifier hv = new HostNameVerifier(){
           @Override
           public boolean verify(    String hostname,    SSLSession session){
                  return true;
           }
        }
        messageSender.setHostnameVerifier(hv);
        uc.setMessageSender(messageSender);
        return uc;
    }

This change should fix the error about PKIX path building failed. I will post the code for this article on github in next few days.

Conclusion –

In short, we showed how to consume a SOAP web service over SSL by adding keystore and truststore check during runtime.

Producing and Consuming SOAP Webservice with Spring Boot – Part V

In last few posts, we have covered following

  1. Spring Boot REST CRUD API – Part I
  2. Swagger Documentation for Spring Boot REST API – Part II
  3. Error Handling and logging in Spring Boot REST API – Part III
  4. Consuming RESTful Webservice – Part IV

In this post, we will describe how to create a SOAP webservice from our existing Spring Boot REST API. This SOAP webservice will provide us user data from the database which is we have connected through Spring-data in Spring REST API.

1. Requirements

  1. Eclipse Mars2
  2. Maven 3.1 and above
  3. Spring 1.4 and above
  4. Java 7
  5. Tomcat 8

2. SOAP Web Service

We will use our existing Spring Boot REST API to build an application that will act as a SOAP web service to provide users data. For a given user id, web service will return user data.

Let’s create a schema file in src/main/resources directory and maven will create java classes based on this schema file.


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="https://betterjavacode.com/benefits/soap"
targetNamespace="https://betterjavacode.com/benefits/soap" elementFormDefault="qualified">
<xs:element name="getUserRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getUserResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="user" type="tns:user"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="user">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="middlename" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="username" type="xs:string"/>
<xs:element name="createdate" type="xs:date"/>
<xs:element name="jobtitle" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

3. Update Maven dependencies

Now to generate classes from schema, we have to make sure we have all the right dependencies in our pom.xml. We will also add spring boot service dependency to create a SOAP web service.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.betterjavacode</groupId>
<artifactId>Benefits</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Benefits Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<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>
<schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>com.betterjavacode.benefits.soap</generatePackage>
<forceRegenerate>true</forceRegenerate>
<scehmas>
<schema>
<url>http://localhost:8080/benefits/endpoints/users.wsdl</url>
</schema>
</scehmas>
</configuration>
</plugin>
</plugins>
<finalName>Benefits</finalName>
</build>
</project>

If we run the project with maven build now, the plugin jaxb2-maven-plugin will generate classes under com.betterjavacode.benefits.soap directory. It will also enable our wsdl SOAP url for users. This will generate following java objects

  • GetUserRequest
  • GetUserResponse
  • ObjectFactory
  • package-info
  • User

4. Defining the service

Next, we will define an interface for our service. This will look like below


package com.betterjavacode.benefits.services;

public interface UserAccountService

{

public com.betterjavacode.benefits.soap.User getUserDetails(int id);
}

Implementation of this service will be mapping out entity class User to generated class for soap service User. Using the id as a key to get user data from repository, we will map to soap service user. For post purposes, we will not show the implementation of this interface.

5. Creating the Service Endpoint

What is a service endpoint? When a SOAP request for defined URL is handled by Spring servlet, Spring servlet redirects that request to service endpoint. Service endpoint then processes that request to create a response. Our spring-boot-starter-web-services dependency will bring all the necessary classes for annotation purposes.


package com.betterjavacode.benefits.services.endpoints;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import com.betterjavacode.benefits.services.UserAccountService;
import com.betterjavacode.benefits.soap.GetUserRequest;
import com.betterjavacode.benefits.soap.GetUserResponse;
import com.betterjavacode.benefits.soap.User;

@Endpoint
public class UserAccountServiceEndpoint

{

// private static final String TARGET_NAMESPACE =              "http://com/betterjavacode/benefits/webservices/useraccountservice";
private static final String TARGET_NAMESPACE =   "https://betterjavacode.com/benefits/soap";

@Autowired
private UserAccountService userAccountService;

@PayloadRoot(localPart = "getUserRequest", namespace = TARGET_NAMESPACE)
public @ResponsePayload GetUserResponse getUserRequest(@RequestPayload    GetUserRequest request)

{
GetUserResponse response = new GetUserResponse();
User user = userAccountService.getUserDetails(request.getId());
response.setUser(user);
return response;
}
}

@Endpoint annotation allows the class to be defined as service endpoint and included in @Component annotation for scanning. Make sure the namespace defined in this class matches with XSD schema definition. Otherwise, you can run into error for “No Endpoint defined for“.

6. Configuration

Next, we will configure our configuration class to generate wsdl endpoint. This configuration class will be annotated by @EnableWs to provide web service configuration.


package com.betterjavacode.benefits;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

@Configuration
@EnableWs
@ComponentScan("com.betterjavacode")
public class AppConfig extends WsConfigurerAdapter {

@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);

return new ServletRegistrationBean(servlet, "/benefits/endpoints/*");
}

@Bean(name = "users")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema usersSchema) {
DefaultWsdl11Definition wsdl11definition = new DefaultWsdl11Definition();
wsdl11definition.setPortTypeName("UserAccountService");
wsdl11definition.setLocationUri("/endpoints");
wsdl11definition.setTargetNamespace("http://com/betterjavacode/benefits/webservices/useraccountservice");
wsdl11definition.setSchema(usersSchema);
return wsdl11definition;
}

@Bean
public XsdSchema usersSchema() {
return new SimpleXsdSchema(new ClassPathResource("employees.xsd"));
}

}

Few important points about this configuration class are

  • MessageDispatcherServlet is a required servlet to dispatch web service messages. We set this servlet with a bean to handle the URL from which request will be coming.
  • DefaultWsdl11Definition creates SOAP for the given XSD schema
  • XsdSchema provides abstraction for our users XSD schema

7. Running the SOAP webservice

Now build our project with maven. Run the spring boot application through eclipse to start the embedded tomcat server. Once the tomcat server starts, if we access url http://localhost:8080/benefits/endpoints/users.wsdl

Output in the browser will be as below

wsdl endpoint

 

Here we showed how to create a simple SOAP webservice which we have combined with Spring Boot REST API service. We can also test this SOAP webservice using Soap UI, as shown in below screenshot

SoapUITest

8. Consuming the SOAP web service

In previous steps, we showed how to produce a SOAP web service, now we will show how to consume this SOAP web service programmatically.

8.1 Create a client class

Under package com.betterjavacode.benefits.views, define a class UserClient which will extend a WebServiceGatewaySupport class. WebServiceGatewaySupport class provides web service methods.


package com.betterjavacode.benefits.views;

import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;

import com.betterjavacode.benefits.soap.GetUserRequest;
import com.betterjavacode.benefits.soap.GetUserResponse;

public class UserClient extends WebServiceGatewaySupport {

public GetUserResponse getUserById(int userid) {
GetUserRequest userrequest = new GetUserRequest();
userrequest.setId(userid);
GetUserResponse response = (GetUserResponse) getWebServiceTemplate().marshalSendAndReceive(userrequest, new SoapActionCallback("http://localhost:8080/benefits/endpoints/getUserResponse"));
return response;
}
}

8.2 Configure the client for Spring Bean support

We will configure Jaxb2Marshaller to support JAXB to set context path. This will help us marshal and unmarshal our xml request and response through.


package com.betterjavacode.benefits.views;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

@Configuration
public class ClientAppConfig {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.betterjavacode.benefits.soap");
return marshaller;
}

@Bean
public UserClient userClient(Jaxb2Marshaller marshaller) {
// WSDL URL - http://localhost:8080/benefits/endpoints/users.wsdl
UserClient uc = new UserClient();
uc.setDefaultUri("http://localhost:8080/benefits/endpoints/users.wsdl");
uc.setMarshaller(marshaller);
uc.setUnmarshaller(marshaller);
return uc;
}
}

 

8.3 Run the SOAP web service client

We will define a class with main method to pass an argument of user id. Our client will call the web service with passed argument to return us the data if that user id existed in the database.


public class UserSoapClient {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(ClientAppConfig.class);
        ctx.refresh();
        UserClient usc = ctx.getBean(UserClient.class);
        System.out.println(" For Employee: ");
        GetUserResponse response = usc.getUserById(1);
        System.out.println("Name: " + response.getUser()
            .getFirstname() + " "
            + response.getUser()
                .getLastname());
        System.out.println(" Job: " + response.getUser()
            .getJobtitle());

    }

}

 

9. Conclusion

In this article, we showed how to create a SOAP web service and how to build a client to consume the same SOAP web service using Spring Boot. The code for this is available at github

10. References

  1. Spring Web services tutorial
  2. SOAP Web service producer and consumer
  3. Consuming a web service in java and SOAP UI

Consuming a RESTful Webservice – Part IV

Continuing the series of posts on Spring Boot, in this post, we will investigate how to consume a REST API service we built previously. This will be a short post, but we will show how to read the data and how to post the data with some of the features Spring Boot offers to consume a REST service for client side. Eventual goal is to use this feature to call our rest service during runtime to use the data from database to display on views which a user will be able to see.

You can read previous posts on this series Part I, Part II and Part III .

Purpose

Purpose of this post is to read company data from Company REST API and also to create a company by posting company data using the same REST API.

Build a client

To consume a rest service programmatically, Spring provides a feature called RestTemplate. RestTemplate is easiest way for client to interact with server side code with just one line of code.

In our client code, we will need a RestTemplate object, REST service URL. Since this is a sample we are building, we will be adding a main method in this class to run this client side of the code. In real life scenarios, during runtime, a client code will call rest template to get server side data, use that data to massage or display to user on user interface.


RestTemplate restTemplate = new RestTemplate();
String resourceAPI_URL = "http://localhost:8080/benefits/v1/companies/{id}";
Company company = restTemplate.getForObject(resourceAPI_URL, Company.class, 1);

This code is showing that we are calling REST service to read company data for company with id that a client will pass.

Similarly, we will have another request to post the data on server side to create a company. The code for that will look like below:


String resourceAPI_POSTURL = "http://localhost:8080/benefits/v1/companies/";
Company comp = new Company();
comp.setName("XYZ Company");
comp.setStatusid(1);
comp.setType("Corporation");
comp.setEin("9343423232");
// comp.setCp(cp);
Company newcomp = restTemplate.postForObject(resourceAPI_POSTURL, comp, Company.class);

In this post, we showed how to use RestTemplate a feature that spring boot provides to consume a REST service. The code for this is available here