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 

猜你喜欢

转载自blog.csdn.net/qiaoqiyu6416/article/details/82661941