Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

Creating a SOAP client for a SOAP web service is very straight forward when using CXF. Given a WSDL url or a WSDL file, create a project with a pom.xml containing the following:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.rightnow.ws</groupId>
    <artifactId>rightnow</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>3.0.4</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>https://twinspires--tst.custhelp.com/cgi-bin/twinspires.cfg/services/soap?wsdl</wsdl>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

When you run mvn compile, it will generate classes in the generated-sources/cxf directory.

The next step is to use the service client. The normal usage pattern is below. The service is instantiated, then the port is retrieved.

package com.rightnow.ws.wsdl.v1_2;

import com.rightnow.ws.metadata.v1_2.MetaDataClass;
import org.junit.Test;

import javax.xml.datatype.XMLGregorianCalendar;
import java.util.List;

import static org.junit.Assert.assertNotNull;

/**
 * @author norris.shelton
 */
public class TestRightNowSyncService {

    @Test
    public void testName() throws Exception {
        RightNowSyncService rightNowSyncService = new RightNowSyncService();
        RightNowSyncPort rightNowSyncPort = rightNowSyncService.getRightNowSyncPort();

        XMLGregorianCalendar metaDataLastChangeTime = rightNowSyncPort.getMetaDataLastChangeTime();
        assertNotNull(metaDataLastChangeTime);

        List<MetaDataClass> metaDataClasses = rightNowSyncPort.getMetaData();
        assertNotNull(metaDataClasses);

    }
}

April 1st, 2015

Posted In: CXF, Java, java ninja, Javaninja, Maven, SOAP

Tags: , , , , ,

2 Comments

UPDATE: THANKS to attel in the comments below, THIS NOW WORKS. I was using the response context object, when I should have been using the request context.

I have a case where the test system is going to be different than the production system. I started with the following in my Springframework config file:

<!--
     Define a cxf endpoint based on client stub generated from a wsdl. It
     is important to provide serviceName and endpointName so the wsdl is
     not needed at runtime. As far as I know the serviceName and
     endpointName do not have to have a special convention but it is good
     practice to use the service namespace and Service Interface name in
     the names
 -->
<jaxws:client id="justiceXchangeService"
              serviceName="namespace:service name"
              endpointName="namespace:endpoint name"
              address="my test service address"
              serviceClass="some.package.ServiceClass">
    <jaxws:properties>
        <!--all validation is turned off-->
        <entry key="set-jaxb-validation-event-handler" value="false"/>
    </jaxws:properties>
</jaxws:client>

This would allow me to inject the client by:

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private ServiceClass serviceClass;

This worked until I had to dynamically specify the service address. I found some documentation that said I should do it like:

        BindingProvider bindingProvider = (BindingProvider) serviceClass;
        bindingProvider.getRequestContext()
                       .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "different service address");

UPDATE: THANKS to attel in the comments below, THIS NOW WORKS. I was using the response context object, when I should have been using the request context.

Unfortunately when I checked my logs, I saw that the address in the original request was still the address declared in the spring config file. Changing the binding didn’t work.

I finally ran across using a proxy. Now my spring context looks like:



This caused some changes in my code. The client is now injected via:

    @Autowired
    private JaxWsProxyFactoryBean jaxWsProxyFactoryBean;

The dynamic service is address is specifyied and the proxy used by:

        jaxWsProxyFactoryBean.setAddress("https://avsdsmv.dfa.arkansas.gov/Appriss/JXC");
        ServiceClass serviceClass = (serviceClass) jaxWsProxyFactoryBean.create();
        serviceClass.wsCall(...);

August 15th, 2012

Posted In: CXF, Java, JAXB, SOAP, Spring

Tags: , , , , ,

4 Comments

Normally, I have been able to generate a Soap client and be on my way. This particular time I had to perform some business logic on the client. One of the first things that I encountered was the nuisance of debugging classes without a toString() implementation. It’s no biggie, but it does waste time when you have to open a class to inspect it’s contents. When it has a toString() implementation, your editor will generally use this to display the class. Making one less step that I have to perform.

The business logic I had to implement involved splitting a list of items into two groups, according to the value of one of the properties. In addition, if the object was already in the list, but had a different address, I had to add the current address to the existing object instead of adding it again. Great. Now I need to be able to identify an object in a list. I found that JAXB-2 Basics added implementations of equals() and hashcode() methods.

Just when I thought I was done, I had to add the serializable interface because the web application is marked as distributable. This wasn’t provided by JAXB2-Basics and I had to use an XML binding to add that.

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.appriss.jxportal</groupId>
        <artifactId>jxportal</artifactId>
        <version>47.0-SNAPSHOT</version>
    </parent>

    <artifactId>tlo-client</artifactId>
    <name>tlo-client</name>

    <properties>
        <cxf.version>2.5.2</cxf.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <!-- Jetty is needed if you're are not using the CXFServlet -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>

        <!--
            Spring dependencies - needed to force a newer version than CXF used
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-runtime</artifactId>
            <version>0.6.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>${cxf.version}</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                        <configuration>
                            <sourceRoot>${basedir}/target/generated-sources/cxf</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <!--note: Started getting errors by having this hit their URL directly-->
                                    <wsdl>https://webservice.tlo.com/TLOWebService.asmx?wsdl</wsdl>
                                    <!--<wsdl>${basedir}/src/main/resources/TLOWebService-20120306.wsdl</wsdl>-->
                                    <bindingFiles>
                                        <bindingFile>${basedir}/src/main/resources/binding.xjb</bindingFile>
                                    </bindingFiles>
                                    <extraargs>
                                        <extraarg>-xjc-XtoString</extraarg>
                                        <extraarg>-xjc-Xequals</extraarg>
                                        <extraarg>-xjc-XhashCode</extraarg>
                                    </extraargs>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.3</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

To use JAXB2-Basics, I added the module dependency starting at line 52. I also added a dependency for the cxf-codegen-plugin at line 93. After that, I had to add the at line 83.

  • -xjc-XtoString – triggers the generation of the toString() method
  • -xjc-Xequals – triggers the generation of the equals() method
  • -xjc-XhashCode – triggers the generation of the hashcode() method

The jaxb binding took a bit more work, but was fairly easy also. I specified the binding file on line 80. ${basedir} is a maven variable that is set to the directory of the module. The binding file is fairly straight forward. I specified a global binding that said serializable. I also specified the optional uid. If the uid is specified, then the value will be specified in the classes. NOTE: that the same uid value will be specified for all classes.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <jaxb:bindings>
        <jaxb:globalBindings>
            <jaxb:serializable uid="1"/>
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings>

April 3rd, 2012

Posted In: Java, Maven, xml

Tags: , ,

Leave a Comment

LinkedIn Auto Publish Powered By : XYZScripts.com