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文件


你可能感兴趣的:(webservice,String,CXF,web服务,jax-ws)