1. 程式人生 > >Servlet容器中編寫WebService

Servlet容器中編寫WebService

Step1. 編寫介面類

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

@WebService //用於標誌介面
public interface Helloworld {
	public String sayHello(@WebParam(name = "text") String text);  //@WebParam標註引數名稱
}

Step2. 編寫介面實現類

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

import test01.service.Helloworld;

@WebService(serviceName = "Helloworld")
public class HelloworldImpl implements Helloworld {

	@Override
	public String sayHello(@WebParam(name = "text") String text) {
		return "Hello "+text;
	}

}

Step3.配置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">
  <display-name>test_cxf_web</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:appliction-context.xml</param-value>
  </context-param>
  
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- CXF Servlet -->
  <servlet>
  	<servlet-name>CXFServlet</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <!-- CXF Servlet Mapping -->
  <servlet-mapping>
  	<servlet-name>CXFServlet</servlet-name>
  	<url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

Step4. application-context.xml

<?xml version="1.0" encoding="UTF-8"?>  
<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-2.5.xsd
      http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd"
    xmlns="http://www.springframework.org/schema/beans">
    
    <!-- 介面實現類  和  訪問地址 -->
    <jaxws:endpoint implementor="test01.service.impl.HelloworldImpl"
    			address="/HelloWorld"></jaxws:endpoint>
    
</beans>

Step5. 部署到Tomcat上,訪問http://localhost:8080/專案名/HelloWorld?wsdl