!!! Beispiel-Listings zu Java-Webservices-Framework
!!! Listing 1: EchoWS.java/Apache Axis2
package de.heise.ix.frameworks;
public class EchoWS {
  public String echo(String text) {
    System.out.println(text);
    return text;
  }
}
!!! Listing 2: services.xml/Apache Axis2
  de.heise.ix.frameworks.EchoWS
  
    
  
!!! Listing 3: LoggingHandler.java/Apache Axis2
package de.heise.ix.frameworks;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.handlers.AbstractHandler;
public class LoggingHandler extends AbstractHandler {
  @Override
  public InvocationResponse invoke(MessageContext context) throws AxisFault {
    System.out.println(context.getEnvelope());
    return InvocationResponse.CONTINUE;
  }
}
!!! Listing 4: EchoWS.java/Apache CXF
package de.heise.ix.frameworks;
public interface EchoWS {
  public String echo(String text);
}
!!! Listing 5: EchoWSImpl.java/Apache CXF
package de.heise.ix.frameworks;
public class EchoWSImpl implements EchoWS {
  @Override
  public String echo(String text) {
    System.out.println(text);
    return text;
  }
}
!!! Listing 6: web.xml/Apache CXF
  
    CXFServlet
    org.apache.cxf.transport.servlet.CXFServlet
    
      config-location
      /WEB-INF/spring-beans.xml    
    
    1
  
  
    CXFServlet
    /services/*
  
!!! Listing 7: spring-beans.xml/Apache CXF
  
    
      
    
  
!!! Listing 8: Client/Apache CXF
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(EchoWS.class);
factory.setAddress("http://localhost:8080/services/echo");
EchoWS echoWS = (EchoWS) factory.create();
System.out.println(echoWS.echo("Hello World!"));   // -> Hello World!
!!! Listing 9: EchoResource.java/Jersey
package de.heise.ix.frameworks;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("/echo")
public class EchoResource {
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String echo(@QueryParam("text") String text) {
    System.out.println(text);
    return text;
  }
}
!!! Listing 10: web.xml/Jersey
  
    RESTServlet
    com.sun.jersey.spi.container.servlet.ServletContainer
    
      com.sun.jersey.config.property.packages
      de.heise.ix.frameworks
    
    1
  
  
    RESTServlet
    /resources/*
  
!!! Listing 11: Server/Jersey
Map initParams = new HashMap();
initParams.put("com.sun.jersey.config.property.packages", "de.heise.ix.frameworks");
SelectorThread selectorThread = GrizzlyWebContainerFactory.create("http://localhost:8080/resources/", initParams);
// ...
selectorThread.stopEndpoint();
!!! Listing 12: Client/Jersey
Client client = Client.create();
WebResource resource = client.resource("http://localhost:8080/resources/echo")
                             .queryParam("text", "Hello World!");
ClientResponse response = resource.get(ClientResponse.class);
System.out.println(response.getStatus());               // -> 200
System.out.println(response.getType());                 // -> text/plain
System.out.println(response.getEntity(String.class));   // -> Hello World!
!!! Listing 13: EchoWS.java/Metro
package de.heise.ix.frameworks;
import javax.jws.WebService;
@WebService
public interface EchoWS {
  public String echo(String text);
}
!!! Listing 14: EchoWSImpl/Metro
package de.heise.ix.frameworks;
import javax.jws.WebService;
@WebService(endpointInterface="de.heise.ix.frameworks.EchoWS",
            serviceName="EchoWSService",
            portName="EchoWSPort")
public class EchoWSImpl implements EchoWS {
  @Override
  public String echo(String text) {
    System.out.println(text);
    return text;
  }
}
!!! Listing 15: sun-jaxws.xml/Metro
  
!!! Listing 16: Client/Metro
URL wsdl = new URL("http://localhost:8080/services/echo?wsdl");
QName serviceName = new QName("http://frameworks.ix.heise.de/", "EchoWSService");
			
Service service = Service.create(wsdl, serviceName);
EchoWS echoWS = service.getPort(EchoWS.class);
System.out.println(echoWS.echo("Hello World!"));   // -> Hello World!
!!! Listing 17: EchoResource.java/RESTEasy
package de.heise.ix.frameworks;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("/echo")
public interface EchoResource {
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String echo(@QueryParam("text") String text);
}
!!! Listing 18: EchoResourceImpl.java/RESTEasy
package de.heise.ix.frameworks;
public class EchoResourceImpl implements EchoResource {
  @Override
  public String echo(String text) {
    System.out.println(text);
    return text;
  }
}
!!! Listing 19: LoggingInterceptor.java/RESTEasy
package de.heise.ix.frameworks;
import javax.ws.rs.WebApplicationException;
import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.core.*;
import org.jboss.resteasy.spi.*;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;
@ServerInterceptor
public class LoggingInterceptor implements PreProcessInterceptor {
  @Override
  public ServerResponse preProcess(HttpRequest request, ResourceMethod method) throws Failure, WebApplicationException {
    System.out.println(request.getUri().getRequestUri());
    System.out.println(method.getMethod().getName());
    return null;
  }
}
!!! Listing 20: Server/RESTEasy
TJWSEmbeddedJaxrsServer server = new TJWSEmbeddedJaxrsServer();
server.setRootResourcePath("/resources");
server.setPort(8080);
server.getDeployment().getActualResourceClasses().add(EchoResourceImpl.class);
server.getDeployment().getActualProviderClasses().add(LoggingInterceptor.class);
server.start();
// ...
server.stop();
!!! Listing 21: Client/RESTEasy
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
EchoResource echoResource = ProxyFactory.create(EchoResource.class, "http://localhost:8080/resources/");
System.out.println(echoResource.echo("Hello World!"));   // -> Hello World!
!!! Listing 22: echo.xsd/Spring-WS
  
    
      
    
  
  
    
      
    
  
!!! Listing 23: EchoWS.java/Spring-WS
package de.heise.ix.frameworks;
import javax.xml.parsers.*;
import org.springframework.ws.server.endpoint.annotation.*;
import org.w3c.dom.*;
@Endpoint                                                                      
public class EchoWS {
  private final static String NAMESPACE_URI = "http://frameworks.ix.heise.de/";
  private final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  @PayloadRoot(namespace = NAMESPACE_URI, localPart = "EchoRequest")
  @ResponsePayload 
  public Element echo(@RequestPayload Element elementEchoRequest) throws ParserConfigurationException {
    String text = elementEchoRequest.getAttribute("text");
    System.out.println(text);
    Document document = factory.newDocumentBuilder().newDocument();
    Element elementEchoResponse = document.createElementNS(NAMESPACE_URI, "EchoResponse"); 
    elementEchoResponse.setAttribute("text", text);
    return elementEchoResponse;
  }
}
!!! Listing 24: LoggingInterceptor.java/Spring-WS
package de.heise.ix.frameworks;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.interceptor.EndpointInterceptorAdapter;
public class LoggingInterceptor extends EndpointInterceptorAdapter {
  @Override
  public boolean handleRequest(MessageContext context, Object endpoint) throws Exception {
    context.getRequest().writeTo(System.out);
    return true;
  }
  @Override
  public boolean handleResponse(MessageContext context, Object endpoint) throws Exception {
    context.getResponse().writeTo(System.out);
    return true;
  }
}
!!! Listing 25: spring-ws-servlet.xml/Spring-WS
  
  
  
  
    
  
  
    
  
!!! Listing 26: Client/Spring-WS
Source source = new StringSource("");
DOMResult result = new DOMResult();
WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
webServiceTemplate.sendSourceAndReceiveToResult("http://localhost:8080/services/echo", source, result);
Element elementEchoResponse = (Element) result.getNode().getFirstChild();
System.out.println(elementEchoResponse.getAttribute("text"));   // -> Hello World!
!!! Ende