spring+maven實現webservice


pom依賴

<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.1.8</version>
</dependency>
<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.1.8</version>
</dependency>

webservice服務端

spring-cxf.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:context="http://www.springframework.org/schema/context"  
   xmlns:jaxws="http://cxf.apache.org/jaxws" 
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                    http://www.springframework.org/schema/context   
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://cxf.apache.org/jaxws  
                    http://cxf.apache.org/schemas/jaxws.xsd">  
     
     <jaxws:endpoint id="helloService" implementor="com.test.cxf.HelloWorldServiceImpl" address="/test"/>
  
</beans>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-cxf.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>
</web-app>

定義webservice接口及實現

@WebService
public interface HelloWorldService {
    
    String sayHello();

}
@WebService
public class HelloWorldServiceImpl implements HelloWorldService {

    @Override
    public String sayHello() {
        System.out.println("進入了遠端服務器");
        return "haha";
    }

}

啟動服務器就行了-----訪問http://localhost:8080/項目名/service/test?wsdl

webservice客戶端(第一種:使用注解)

spring-cxf.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:context="http://www.springframework.org/schema/context"  
   xmlns:jaxws="http://cxf.apache.org/jaxws" 
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                    http://www.springframework.org/schema/context   
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://cxf.apache.org/jaxws  
                    http://cxf.apache.org/schemas/jaxws.xsd">  
     
     <jaxws:client id="helloService" serviceClass="com.test.IHelloService" address="http://localhost:8080/cxf-server/service/test?wsdl"></jaxws:client>
  
</beans>

自定義與服務端相同接口下的方法

@WebService(targetNamespace="http://cxf.test.com/",name="HelloWorldService")
public interface IHelloService {
    
    public String sayHello();

}

測試

@Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-cxf.xml");
        IHelloService service = (IHelloService)context.getBean("helloService");
        System.out.println(service.sayHello());
    }

webservice客戶端(第二種:命令行引入Java代碼)

打開cmd,輸入命令行

wsimport -s 本地路徑 -p 包名 -keep wsdl路徑名

解析成功后看到如下目錄

測試

@Test
    public void test1() {
        HelloWorldService he = new HelloWorldServiceImplService().getHelloWorldServiceImplPort();
        he.sayHello();
        
    }

大功告成!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM