Jorge Garcia
Jorge Garcia

Reputation: 65

How to create and host a SOAP web service in Google App Engine?

It is possible create and host a SOAP web service in a Google App Engine, using Java?

I've been looking at the official GAE documentation and the internet, but I can't find information or instructions about it.

If possible, what are the libraries to add in the pom.xml and the configuration instructions for the application (for the web.xml and appengine-web.xml)?

Upvotes: 1

Views: 1200

Answers (1)

Jose Henriquez
Jose Henriquez

Reputation: 376

The short answer is yes. The main thing to understand is that AppEngine Java environment uses Jetty HTTP/servlet container. From there, you can follow the Metro user guide to add the web service to your site/application. Metro is the JAX-WS implementation. Here is what I did to add a SOAP web service to the GuessBook sample web site, which I ran in the local development environment - I was bit lazy and I didn't want to start from scratch:

  1. Add the Metro dependency to your POM file:

    <dependency>
        <groupId>org.glassfish.metro</groupId>
        <artifactId>webservices-rt</artifactId>
        <version>2.3</version>
    </dependency>
    
  2. Create a standard SOAP web service class that will handle the incoming web service calls. For example:

    package com.jh.guessbook;
    
    import javax.jws.WebService;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    
    @WebService(serviceName = "SOAPHelloWorld")
    public class SOAPHelloWorld
    {
        /**
         * This is a sample web service operation
         */
        @WebMethod(operationName = "hello")
        public String hello(@WebParam(name = "name") String txt)
        {
            return "Hello " + txt + " !";
        }
    }
    
  3. Create a sun-jaxws.xml file in your WEB-INF folder. This is where you define your endpoints. Here is the file for my sample web service:

    <?xml version="1.0" encoding="UTF-8"?>
    <endpoints
            xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
            version="2.0">
        <endpoint
                name="SOAPHelloWorld"
                implementation="com.jh.guessbook.SOAPHelloWorld"
                url-pattern="/soapws"/>
    </endpoints>
    
  4. Last but not least, setup the WS listener, servlet, and URL mapping in the web.xml. Here is what I added to my web.xml:

    <listener>
        <listener-class>
             com.sun.xml.ws.transport.http.servlet.WSServletContextListener
          </listener-class>
    </listener>
    <servlet>
        <servlet-name>SOAPHelloWorld</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
        <servlet-name>SOAPHelloWorld</servlet-name>
        <url-pattern>/soapws</url-pattern>
     </servlet-mapping>
    

You can get the WSDL definition by browsing your endpoint with ?wsdl. In my case, again running locally, the URL is http://localhost:8080/soapws?wsdl.

Upvotes: 3

Related Questions