加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

CXF实现WebService

发布时间:2020-12-17 00:00:20 所属栏目:安全 来源:网络整理
导读:首先下载cxf包,目前最新的版本是 apache-cxf-2.1.,下栽地址http://cxf.apache.org/download.html。 1. 首先新建一个web工程CxfService,导入cxf所需要的包。要导入的包如下: commons-logging-1.1.jargeronimo-activation_1.1_spec-1.0-M1.jar (or Sun's A

首先下载cxf包,目前最新的版本是apache-cxf-2.1.,下栽地址http://cxf.apache.org/download.html。

1. 首先新建一个web工程CxfService,导入cxf所需要的包。要导入的包如下:

commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.1.jar
jaxb-impl-2.1.6.jar
jaxws-api-2.1.jar
jetty-6.1.5.jar
jetty-util-6.1.5.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar

The Spring jars (optional - for XML Configuration support):

aopalliance-1.0.jar
spring-core-2.0.4.jar
spring-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-web-2.0.4.jar

And the CXF jar:

cxf-2.1.jar
2.新建一个接口类:HelloWorld,如下:
Java代码

  1. package com.zx.cxf.service; ?

  2. import javax.jws.WebParam; ?

  3. import javax.jws.WebService; ?

  4. @WebService

  5. publicinterface HelloWorld { ?

  6. ?String sayHi(@WebParam(name="text") String text); ?

  7. }

package com.zx.cxf.service;

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

@WebService
public interface HelloWorld {
    String sayHi(@WebParam(name="text") String text);
}
创建接口的实现类:HelloWorldImpl,如下
Java代码

  1. package com.zx.cxf.service; ?

  2. import javax.jws.WebService; ?

  3. import com.zx.cxf.service.HelloWorld; ?

  4. @WebService(endpointInterface = "com.zx.cxf.service.HelloWorld",?

  5. ? ? ?serviceName = "HelloWorld") ?

  6. publicclass HelloWorldImpl implements HelloWorld { ?

  7. public String sayHi(String text) { ?

  8. return"Hello " + text; ?

  9. ?} ?

  10. }

package com.zx.cxf.service;

import javax.jws.WebService;

import com.zx.cxf.service.HelloWorld;
@WebService(endpointInterface = "com.zx.cxf.service.HelloWorld",serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
 
    public String sayHi(String text) {
        return "Hello " + text;
    }
}
*@WebService:申明为webservice的注解
*endpointInterface:要暴露的接口类
*serviceName : ?服务名
在WEB-INF目录下新建beans.xml,如下:
Xml代码

  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <!-- START SNIPPET: beans -->

  3. <beansxmlns="http://www.springframework.org/schema/beans"

  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  5. xmlns:jaxws="http://cxf.apache.org/jaxws"

  6. xsi:schemaLocation=" ?

  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ?

  8. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

  9. <importresource="classpath:META-INF/cxf/cxf.xml"/>

  10. <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>

  11. <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>

  12. <jaxws:endpoint

  13. id="helloWorld"

  14. implementor="com.zx.cxf.service.HelloWorldImpl"

  15. address="/HelloWorld"/>

  16. </beans>

  17. <!-- END SNIPPET: beans -->

<?xml version="1.0" encoding="UTF-8"?>

<!-- START SNIPPET: beans -->
<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" />

	<jaxws:endpoint 
	  id="helloWorld" 
	  implementor="com.zx.cxf.service.HelloWorldImpl" 
	  address="/HelloWorld" />
	  
</beans>
<!-- END SNIPPET: beans -->
注: implementor :接口类的实现类
? ?address: ?要和注释里面神秘的服务名对应,
修改web.xml文件,如下:
Xml代码

spinner.gif

  1. <?xmlversion="1.0"encoding="ISO-8859-1"?>

  2. <!DOCTYPE web-app ?

  3. ?PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN" ?

  4. ?"http://java.sun.com/dtd/web-app_2_3.dtd">

  5. <!-- START SNIPPET: webxml -->

  6. <web-app>

  7. <context-param>

  8. <param-name>contextConfigLocation</param-name>

  9. <param-value>/WEB-INF/beans.xml</param-value>

  10. </context-param>

  11. <listener>

  12. <listener-class>

  13. ? ? ?org.springframework.web.context.ContextLoaderListener ?

  14. </listener-class>

  15. </listener>

  16. <servlet>

  17. <servlet-name>CXFServlet</servlet-name>

  18. <display-name>CXF Servlet</display-name>

  19. <servlet-class>

  20. ? ? ?org.apache.cxf.transport.servlet.CXFServlet ?

  21. </servlet-class>

  22. <load-on-startup>1</load-on-startup>

  23. </servlet>

  24. <servlet-mapping>

  25. <servlet-name>CXFServlet</servlet-name>

  26. <url-pattern>/services/*</url-pattern>

  27. </servlet-mapping>

  28. </web-app>

  29. <!-- END SNIPPET: webxml -->

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<!-- START SNIPPET: webxml -->
<web-app>
	<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>
		<display-name>CXF Servlet</display-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
</web-app>
<!-- END SNIPPET: webxml -->
启动tomcat
测试:简单的测试就是ie测试,在浏览器中输入 http://localhost:8080/CxfService/services/,如果出现

{http://service.cxf.zx.com/}HelloWorldImplPort ,或者输入http://localhost:8080/CxfService/services/HelloWorld?wsdl,出现wsdl文挡,则说明服务器端配置成功。

可户端测试:

测试类如下:

Java代码

  1. package com.zx.cxf.service; ?

  2. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; ?

  3. import org.apache.cxf.interceptor.*; ?

  4. import com.zx.cxf.service.HelloWorld; ?

  5. publicclass client { ?

  6. private client() { ?

  7. ?} ?

  8. publicstaticvoid main(String args[]) throws Exception { ?

  9. ? ?JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); ?

  10. ? ?factory.getInInterceptors().add(new LoggingInInterceptor()); ?

  11. ? ?factory.getOutInterceptors().add(new LoggingOutInterceptor()); ?

  12. ? ?factory.setServiceClass(HelloWorld.class); ?

  13. ? ?factory.setAddress("http://localhost:8080/CxfService/services/HelloWorld"); ?

  14. ? ?HelloWorld client = (HelloWorld) factory.create(); ?

  15. ? ?String reply = client.sayHi("HI"); ?

  16. ? ?System.out.println("Server said: " + reply); ?

  17. ?} ?

  18. }

package com.zx.cxf.service;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.interceptor.*;
import com.zx.cxf.service.HelloWorld;
public  class client {

   

    private client() {
    } 

    public static void main(String args[]) throws Exception {
    	JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    	factory.getInInterceptors().add(new LoggingInInterceptor());
    	factory.getOutInterceptors().add(new LoggingOutInterceptor());
    	factory.setServiceClass(HelloWorld.class);
    	factory.setAddress("http://localhost:8080/CxfService/services/HelloWorld");
    	HelloWorld client = (HelloWorld) factory.create();

    	String reply = client.sayHi("HI");
    	System.out.println("Server said: " + reply);
    }

}

如果控制台打印出:Server said: Hello HI则说明测试成功。

Ps:如果出现in thread "main" javax.xml.ws.soap.SOAPFaultException: Error reading XMLStreamReader.

关掉防火墙就可以了。

下面是源文件:下载后倒入所需要的包

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读