Creating the Web Service Provider (WSP)
This class, kept in the service submodule's src/main/java/service folder, is commonly known as either the Service Implementation Bean (SIB) or the SEI (Service Endpoint Interface) implementation. The SEI is the DoubleItPortType class that was generated from the WSDL earlier. The methods in the SEI map to the operations defined in the portType section of the WSDL.
package service;
import javax.jws.WebService;
import org.example.contract.doubleit.DoubleItPortType;
@WebService(targetNamespace = "http://www.example.org/contract/DoubleIt",
portName="DoubleItPort",
serviceName="DoubleItService",
endpointInterface="org.example.contract.doubleit.DoubleItPortType")
public class DoubleItPortTypeImpl implements DoubleItPortType {
public int doubleIt(int numberToDouble) {
return numberToDouble * 2;
}
}
We should also create JUnit unit tests of our web service implementation in which we check before deploying the SIB that its methods are properly implemented (e.g., doubleIt is not erroneously tripling incoming numbers). Sample test cases for the SIB are placed in the same Java package as the class we're testing albeit in a different folder location (per Maven convention, service/src/test/... instead of service/src/main/...). Doing it this way reduces the need for Java import statements in the test cases while still keeping test code out of deployment JARs. The following unit test cases are included for this sample:
package service;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DoubleItPortTypeImplTest {
@Test
public void testDoubleItWorksWithPositiveNumbers() {
DoubleItPortTypeImpl port = new DoubleItPortTypeImpl();
int response = port.doubleIt(12);
assertEquals("DoubleIt isn't working with positive numbers", 24, response);
}
@Test
public void testDoubleItWorksWithZero() {
DoubleItPortTypeImpl port = new DoubleItPortTypeImpl();
int response = port.doubleIt(0);
assertEquals("DoubleIt isn't doubling zero correctly", 0, response);
}
@Test
public void testDoubleItWorksWithNegativeNumbers() {
DoubleItPortTypeImpl port = new DoubleItPortTypeImpl();
int response = port.doubleIt(-8);
assertEquals("DoubleIt isn't working with negative numbers", -16, response);
}
}
During the build process (mvn clean install) JUnit tests will be automatically detected and run before any JARs are created. If there's any failure in the test cases the build will halt, requiring you to fix the SIB prior to re-running the build process. If failures occur, check the service/target/surefire-reports folder that will be created for detailed test results.
Later, you may also wish to do integration testing of your web service, using actual SOAP calls against a web service activated via an embedded (internal) server. For an example, the java_first_jaxws example in the software distribution <TalendRuntimePath>/examples/apache/cxf/java_first_jaxws configures separate Maven profiles within the pom.xml, one for the service and the other for a test client. Simply running the mvn -Pserver and mvn -Pclient commands from separate terminal windows will allow you to see the results of client requests against the web service provider.