2011年11月2日 星期三

[Java] JAX-WS Using Spring Framework

Step-by-step:
  1. In Eclipse, new a dynamic web project, say EchoService
  2. Provide an interface for EchoService, say EchoService.java, in a custom demo package.
    package demo;
    
    public interface EchoService {
        
        public String echo(String word);
        
    }
  3. Provide an implementation for EchoService interface, say EchoServiceImpl.java.
    package demo;
    
    public class EchoServiceImpl implements EchoService {
    
        public String echo(String word) {
            return word;
        }
    
    }
  4. Provide an endpoint to exporting EchoService web service. Make sure that all Spring framework JAR files and commons-logging JAR file are in the build path.
    package demo;
    
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    @WebService
    (
        targetNamespace = "http://demo/", 
        serviceName="EchoService"
    )
    public class EchoServiceEndpoint {
    
        @Autowired 
        private EchoService biz;
        
        @WebMethod
        public String echo(
                @WebParam(name = "word") String word
        ) {
            return biz.echo(word);
        }
        
    }
  5. Provide applicationContext.xml.
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <context:annotation-config />
        <bean id="echoService" class="demo.EchoServiceImpl" />
    
        <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
            <property name="baseAddress" value="http://localhost:8888/" />
        </bean>
    
        <bean id="echoServiceEndpoint"
            class="demo.EchoServiceEndpoint">
        </bean>
    </beans>
    
  6. Provide a standalone web service publisher, say Main.java.
    package demo;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
    
        private static final ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "demo/applicationContext.xml");
    
        public static void main(String[] args) {
            System.out.println("Start Standalone JAX-WS Server...");
        }
    
    }
  7. Run as Java application (demo.Main). Please check http://localhost:8888/EchoService?wsdl.
Enjoy!


Reference:

沒有留言:

張貼留言