How to make a culinary client web service in Java?

We are currently using Axis2 in our facility. Our project involves calling several web services for troubleshooting. The problem is that very often we have changes in WSDL (which are not life-threatening, since they are simply added to new data types and services, something very rare), which is why we also need to update our application. Basically, we need to get a new copy of WSDL, run it through WSDL2java and use new banks, run our unit tests and pack new banks and set them up for production.

Although the ability to generate stubs in the compilation command gives us xmlbeans, which we can easily work with in our DAO java code, this compilation-deployment cycle, due to WSDL, consumes command time. I was wondering if any changes are possible? Are there any Java APIs that can generate stubs at runtime or provide an unclosed call to a web service, but still give us the ability to work with Java objects rather than documents to process documents? Something like this site for soap . Should I just indicate the location of the WSDL, and should I just get an object with which I can access the document (both the request and the response), and also be able to change the location of the WSDL at runtime?

Not sure if this type of runtime behavior is possible in Java, since objects created at runtime will have different types, etc.? not sure ... I saw some Groovy examples that come close to what I want, but using Groovy means an architectural change for us .. and it's a bit complicated ... Is there any Java / API library?

+3
source share
3 answers

Eclipse Web Service Explorer builds an arbitrary WSDL client interface on the fly. Therefore, I suppose that what you want to do is possible if you are ready for your application to use a little reflection to call services.

Eclipse , , ?

, ant, WSDL , . , .

+1

, Spring JaxWsPortProxyFactoryBean. , . URL- . ( ), , .

WSDL, , , wsdl2java, , beans . , , , .

0
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

/**
 * This class creates SOAP clients from WDSL and a Java interface.
 * See http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXRPC5.html
 * <p>
 * Consider the following sample usage:
 * <pre>
    URL            wsdl = new URL("http://localhost:8080/calc/calc?wsdl");
    String  serviceName = "CalculatorWSService";
    String nameSpaceURI = "http://calculator.me.org/";
    String     portName = "CalculatorWSPort";
    Calculator     calc = SOAPClient.newInstance(wsdl, nameSpaceURI, serviceName, portName, Calculator.class);
 * </pre>
 * @author Curt
 */
public final class SOAPClient {

    /**
     * Create a new SOAPClient, given the specified parameters.
     * @param url where the WSDL is
     * @param nameSpaceUri
     * @param serviceName
     * @param portName
     * @param face interface to use
     * @return an object that implements the interface and is connected to the server
     */
    public static <T> T newInstance(
        URL url, String nameSpaceUri, String serviceName,
        String portName, Class<T> face)
        throws RemoteException
    {
        try {
            QName portQname    = new QName(nameSpaceUri, portName);
            QName serviceQname = new QName(nameSpaceUri, serviceName);
            Service service = Service.create(url, serviceQname);
            T remote = service.getPort(portQname,face);
            T proxy = face.cast(remote);
            return proxy;
        } catch (Throwable t) {
            String message =
                "Connecting to URL=" + url +
                " name space URI= "+ nameSpaceUri +
                " service name=" + serviceName +
                " interface=" + face +
                " port=" + portName;
            throw new RemoteException(message,t);
        }
    }

    /**
     * Don't specify the portName and trust that the service will do it.
     */
    public static <T> T newInstance(
        URL url, String nameSpaceUri, String serviceName, Class<T> face)
        throws MalformedURLException, RemoteException
    {
        QName serviceQname = new QName(nameSpaceUri, serviceName);
        Service service = Service.create(url, serviceQname);
        T remote = service.getPort(face);
        T proxy = face.cast(remote);
        return proxy;
    }
}

An undocumented class of service can provide what you are looking for.

0
source

Source: https://habr.com/ru/post/1727581/


All Articles