WebService

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen

Die hier gezeigte Lösung lief auf mehreren Servern gleichermaßen und ohne weitere Pakete einbinden zu müssen.


Als erstes benötigen wir ein Interface des WebServices, worüber dem Client die möglichen Methoden angeziegt werden. <code=java> package webTest;


import javax.jws.*; import javax.jws.soap.*;

import java.util.*; /**

*
* @author seann
*/

@WebService @SOAPBinding(style = SOAPBinding.Style.DOCUMENT) public interface WebTest {

   @WebMethod
   public ArrayList<String> check(ArrayList<String> list);

} </code=java>

Die Klasse DefaultWebTest implementiert das Interface WebTest. Wie man hier sieht, übergebe ich als Parameter ein komplexes Object Typ ArrayList und stelle ein solches Object auch wieder für den Clienten (Aufrufer) bereit. Dies betone ich deshalb, da ich die auch mit Axis Version 2.2.8 versucht hatte, dies aber nur mit einfachen Datentypen gelang.

<code=java>

package webTest;

import javax.jws.soap.*; import javax.xml.ws.soap.*; import javax.jws.*; //import javax.ejb.*;

import java.util.*;

/**

*
* @author seann
*/

@WebService(serviceName="WebTestService"

          ,portName="WebTestPort"
          ,endpointInterface="webTest.WebTest"
          ,targetNamespace="http://webTest"
       )

//@Stateless public class DefaultWebTest implements WebTest {

   public ArrayList<String> check( ArrayList<String> list) 
   {
       list.add("return");
       return list;
   }

}

</code=java>

<code=java> package webTest;

import javax.xml.ws.Endpoint; import javax.servlet.*;

/**

*
* @author seann
*/

@javax.servlet.annotation.WebListener public class AppServletContextListener implements javax.servlet.ServletContextListener {

   Endpoint endpoint = null;
   
   public void contextInitialized(ServletContextEvent sce) 
   { 
       String url = sce.getServletContext().getInitParameter("url");
       //Endpoint.publish("{protocol}://{host}:{port}/{context}/{wsName}", new DefaultWebTest());
       if(url != null)
       {
           endpoint = Endpoint.publish(url, new DefaultWebTest());
       }
       else
       {
           endpoint = Endpoint.publish("http://:6038/WebTest/WebTestService", new DefaultWebTest());
       }        
   } 
   public void contextDestroyed(ServletContextEvent sce) 
   { 
       endpoint.stop();
   }

}

</code=java>

<code=xml> <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5"

           xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    
   <context-param>
       <param-name>url</param-name>
       <param-value>http://:8080/blupp</param-value>
   </context-param>
   
   <listener>
           <listener-class>webTest.AppServletContextListener</listener-class>    
   </listener>
 	
   <session-config>
       <session-timeout>
           30
       </session-timeout>
   </session-config>

</web-app> </code=xml>


<code=java> package webtestclient;


import java.net.URL; import java.util.ArrayList; import javax.xml.namespace.QName; import javax.xml.ws.Service;

/**

*
* @author seann
*/

public class WebTestClient {


   public WebTestClient() 
   {
       
   }
   /**
    * @param args the command line arguments
    */
   public static void main(String[] args)
   {
       // TODO code application logic here
       
       WebTestClient client = new WebTestClient();
       client.connect();
   }
   
   public void connect()
   {
       try {
           String serviceName = "WebTestService";        
           String servicePort = "WebTestPort";
           String url = "http://:6040/WebTest/WebTestService";
           url = "http://:8080/blupp";
           //url = "http://localhost:8041/axis2/services/WebTestService?wsdl";
           String namespace = "http://webTest";
           Service service = Service.create(new URL(url + "?wsdl") , new QName(namespace , serviceName));
           webTest.WebTest proxy = service.getPort(new QName(namespace, servicePort), webTest.WebTest.class);
           
           ArrayList<String> list = new ArrayList<String>();
           
           list.add("start");
           
           ArrayList<String> result = proxy.check(list);
           //ArrayList<String> result = ((webTest.WebTest)proxy).check(list);
           if(result.size() > 0)
           {
               for(int index = 0; index < result.size(); index++)
               {
                   System.out.println(result.get(index));
               }
           }
       }
       catch(Exception e) 
       {
           e.printStackTrace();
       }
   }

} </code=java>


--JSeann (25.10.2013)