WebService的实现——cxf
发布时间:2020-12-17 00:04:12 所属栏目:安全 来源:网络整理
导读:一、所需的jar包(http://download.csdn.net/detail/huangzebiao007/6380241) 二、实例 1、创建对外发布的接口和实现类 package com.hzb;import javax.jws.WebService;@WebServicepublic interface HelloWorld {public String sayHello(String name);} packag
一、所需的jar包(http://download.csdn.net/detail/huangzebiao007/6380241) 二、实例 1、创建对外发布的接口和实现类 package com.hzb; import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHello(String name); } package com.hzb; import javax.jws.WebService; public class HelloWorldImpl implements HelloWorld { public String sayHello(String name) { return name+":hello world!"; } }2、创建服务发布类 package com.hzb; import javax.xml.ws.Endpoint; public class WebServiceApp { ?? ?? public static void main(String[] args) { ????????? System.out.println("web service starting"); ????????? HelloWorldImpl implementor= new HelloWorldImpl(); ????????? String address="http://localhost:8080/helloWorld"; ????????? Endpoint.publish(address,implementor); ????????? System.out.println("web service started"); ?? ?? } }3、创建客户端 package com.hzb; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class HelloWorldClient { public static void main(String[] args) { JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean(); svr.setServiceClass(HelloWorld.class); svr.setAddress("http://localhost:8080/helloWorld"); HelloWorld hw = (HelloWorld) svr.create(); System.out.println(hw.sayHello("hzb")); } } 先启动发布类,然后在启动客户端,输出:hzb:hello world! 三、Spring集成cxf 1、创建一个WebServiceCXF项目用于发布webservice,引入上面的jar包 (1)创建接口 package com.cxf; import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHello(String name); } (2)创建接口实现类 package com.cxf; import javax.jws.WebService; public class HelloWorldImpl implements HelloWorld { ?? ?public String sayHello(String name) { ?? ??? ?return name+":hello world!"; ?? ?} } (3)配置applicationContext.xml2、创建一个WebServiceClient客户端项目用于访问webservice,引入上面的jar包和引入上面的整个项目<?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" /> ?? ? ?? ?<!-- implementorClass指向该接口的实现类, ?? ?address指定该接口暴露的地址(前面是http://ip:port/project/web.xml中配置的访问前缀) --> ?? ?<jaxws:endpoint id="helloWorld" ?? ??? ?implementorClass="com.cxf.HelloWorldImpl" ?? ??? ?address="/HelloWorld" /> ?? ??? ? </beans> (4)在web.xml文件中配置cxf和spring <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" ?? ?xmlns="http://java.sun.com/xml/ns/javaee" ?? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ?? ?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ?? ?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> ? <welcome-file-list> ?? ?<welcome-file>index.jsp</welcome-file> ? </welcome-file-list> ?? ?<context-param> ?? ??? ?<param-name>contextConfigLocation</param-name> ?? ??? ?<param-value> classpath:/applicationContext.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>/webservice/*</url-pattern> ?? ?</servlet-mapping>? ? </web-app> 项目部署到web服务器后,就可以通过http://localhost:8080/WebServiceCXF/webservice访问到发布的所有接口了 (1)配置applicationContext.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"> ?<!-- serviceClass是所要访问的webService接口,而address是要访问的接口地址 --> <jaxws:client id="helloClient" serviceClass="com.cxf.HelloWorld" address="http://localhost:8080/WebServiceCXF/webservice/HelloWorld" /> </beans> (2)创建client访问类 package com.hzb; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cxf.HelloWorld; public class Client { ?? ?public static void main(String[] args) { ???? ? ? ??????? ApplicationContext context = new ClassPathXmlApplicationContext( ? ??????????????? "applicationContext.xml"); ? ??????? HelloWorld client = (HelloWorld) context.getBean("helloClient"); ? ??????? String message = client.sayHello("hzb"); ? ??????? System.out.println(message); ? ??? } ? } 运行该类,得到结果:hzb:hello world! 注意:上面是假设webservice发布是能拿到整个项目,所以客户端访问才能直接导入该包,如果不是的话,那就要通过访问wsdl文件,然后根据该文件得到接口。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |