微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CXF-HelloWorld-服务端

第一步:编写服务接口(SEI)

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(portName="HelloWorldPortType",name="HelloWorldPortType")
public interface HelloWorld {

	@WebMethod(operationName="sayHello")
	public @WebResult(name="sayHelloResponse")String sayHelloWorld(@WebParam(name="name")String name);
}

代码中使用了jax-ws中的注释来表明服务接口及如何在服务中描述方法(operation)和它的入参与返回对象


第二步:实现接口(业务逻辑)

import java.io.FileNotFoundException;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerfactorybean;


public class HelloWorldImpl implements HelloWorld{


	public String sayHelloWorld(String name) {
	
		return name+" say hello to world!";
	}

第三步:发布服务

(一):利用CXF的JaxWsServerfactorybean在main()中发布

public static void main(String[] args) throws FileNotFoundException {
		JaxWsServerfactorybean serverfactorybean=new JaxWsServerfactorybean();
		serverfactorybean.setServiceClass(HelloWorldImpl.class);
                //注意这里添加拦截器,实现日志输出(可以在控制台看到请求与相应信息)
		serverfactorybean.getininterceptors().add(new LoggingInInterceptor());
		serverfactorybean.getoutInterceptors().add(new LoggingOutInterceptor());
		serverfactorybean.setAddress("http://localhost:8080/ws/HelloWorld");
		serverfactorybean.create();
	}
}


(二):整合spring在tomcat上发布:

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>CXFServer</display-name>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/beans.xml</param-value>
 </context-param>
 
 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <servlet>
  <servlet-name>CXFServlet</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>CXFServlet</servlet-name>
 <url-pattern>/ws/*</url-pattern>
 </servlet-mapping>
</web-app>


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"
    xmlns:cxf="http://cxf.apache.org/core"
    xmlns:wsa="http://cxf.apache.org/ws/addressing"
	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"
	default-lazy-init="true">


	<import resource="classpath:meta-inf/cxf/cxf.xml"></import>
	<import resource="classpath:meta-inf/cxf/cxf-extension-soap.xml"></import>
	<import resource="classpath:meta-inf/cxf/cxf-servlet.xml"></import>

	<bean id="helloService" class="com.server.test.hellowWorld.HelloWorldImpl"></bean>
	<bean id="LoggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
	<bean id="LoggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>


	<jaxws:server id="helloServiceWs" address="/helloService"
		serviceBean="#helloService">
		 <jaxws:inInterceptors> 
		 <ref bean="LoggingInInterceptor"></ref> 
		 </jaxws:inInterceptors> 
		 
		 <jaxws:outInterceptors> 
		 <ref bean="LoggingOutInterceptor"></ref> 
		 </jaxws:outInterceptors> 
	</jaxws:server>
</beans>
运行main方法或启动tomcat,在浏览器地址栏中输入:http://localhost:8080/ws/HelloWorld?wsdl即可获取wsdl文件

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐