servlet容器中webservice的實現

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>WEB-INF/server-bean.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>/*</url-pattern>
    </servlet-mapping>
</web-app>


server-bean.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:jaxws="http://cxf.apache.org/jaxws"  
       xsi:schemaLocation="  
             http://www.springframework.org/schema/beans  
             http://www.springframework.org/schema/beans/spring-beans.xsd  
             http://cxf.apache.org/jaxws   
             http://cxf.apache.org/schemas/jaxws.xsd">  
 
      <import resource="classpath:META-INF/cxf/cxf.xml"/>  
      <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
      <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
      
      <jaxws:endpoint id="LoginInterface" implementor="com.demo.LoginImpl" address="/login"/>
</beans>


webservice接口

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface LoginInterface {

    String setName(@WebParam(name="name")String name);
}

webservice實現

import javax.jws.WebService;

@WebService
public class LoginImpl implements LoginInterface {

    public String setName(String name) {
        System.out.println("asasasas");
        return name;
    }

}


打包到tomcat服務器中發佈,訪問地址:http://localhost:8080/Demo_CXF/login?wsdl


客戶端:

client.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:jaxws="http://cxf.apache.org/jaxws"  
       xsi:schemaLocation="  
             http://www.springframework.org/schema/beans  
             http://www.springframework.org/schema/beans/spring-beans.xsd  
             http://cxf.apache.org/jaxws   
             http://cxf.apache.org/schemas/jaxws.xsd">  
 
      <import resource="classpath:META-INF/cxf/cxf.xml"/>  
      <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
      <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
      
      <jaxws:client id="Client" serviceClass="com.demo.LoginInterface" address="http://localhost:8080/Demo_CXF/login"/>
</beans>

客戶端代碼:

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.demo.LoginInterface;
public class Client {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"com/demo/client/server-bean.xml"});
        LoginInterface loginInterface = (LoginInterface) context.getBean("Client");
        System.out.println(loginInterface.setName("asdasdasdasdasdasd"));
    }
}

運行得到結果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章