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

使用cxf创建webservice

发布时间:2020-12-17 00:33:43 所属栏目:安全 来源:网络整理
导读:1、 ?? 创建web项目。 2、 ? ?引入spring,cxf包。 3、 ? 创建一个服务的终端接口(SayHello) package zxf.cxf.service; import javax.jws.WebMethod; import javax.jws.WebService; ?@WebService public interface? SayHello { ??? ?@WebMethod ??? ?publi

1、?? 创建web项目。

2、? ?引入spring,cxf包。

3、? 创建一个服务的终端接口(SayHello)

package zxf.cxf.service;

import javax.jws.WebMethod;

import javax.jws.WebService;

?@WebService

public interface? SayHello {

??? ?@WebMethod

??? ?public String say(String name);

}

4、? 创建服务实现类(SayHello)

package zxf.cxf.service;

?

public class SayHelloImpl implements SayHello {

@Override

?????? public String say(String name) {

????????????? System.out.println("hello");

????????????? return "hello "+name;

?????? }

}

5、? 创建服务器bean的配置文件(bean.xml)

<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-3.0.xsd

????????????????????????????????????????? http://cxf.apache.org/jaxws

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

?????? <!--下面的配置是为了创建cxf需要的bean对象? -->

?????? <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终端 -->

?????? <jaxws:endpoint id="helloService" implementor="zxf.cxf.service.SayHelloImpl"

????????????? address="/sayHello" />

</beans>

6、? 配置web文件(配spring的监听器和cxf的servlet)

<?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_2_5.xsd" id="WebApp_ID" version="2.5">

? <display-name>cxf_service</display-name>

? <welcome-file-list>

??? <welcome-file>index.html</welcome-file>

??? <welcome-file>index.htm</welcome-file>

??? <welcome-file>index.jsp</welcome-file>

??? <welcome-file>default.html</welcome-file>

??? <welcome-file>default.htm</welcome-file>

??? <welcome-file>default.jsp</welcome-file>

? </welcome-file-list>

? <!-- 通过上文参数指定spring配置文件的位置 -->

?????? <context-param>

????????????? <param-name>contextConfigLocation</param-name>

????????????? <param-value>classpath:beans.xml</param-value>

?????? </context-param>

??????

?????? <!-- 配置spring上下文载入器监听器,确保web服务器启动时,通知完成spring容器的初始化. -->

?????? <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>

?????? <servlet-mapping>

????????????? <servlet-name>CXFServlet</servlet-name>

????????????? <url-pattern>/*</url-pattern>

?????? </servlet-mapping>

</web-app>

7、? 部署web服务到tomcat中去。查看服务文档。

8、? 查看webservice的描述文档http://localhost:8080/cxf_service/helloService?wsdl

9、? 创建webservice的克服端程序。

(1)?????? 引入类库。Cxf+Spring

(2)?????? 创建Spring配置文件clent.xml

<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">

?????? <jaxws:client id="sayHelloServiceClient"

????????????? serviceClass="zxf.cxf.service.SayHello"

????????????? address="http://localhost:8080/cxf_service/helloService" />

</beans>

(3)将sayHello接口复制过来。

(4)新建app客户端程序

package zxf.cxf.service;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

?????? public static void main(String[] args) {

????????????? System.out.println(System.getProperty("java.endorsed.dirs"));

????????????? ApplicationContext ac=new ClassPathXmlApplicationContext("client.xml");

????????????? SayHello s=(SayHello)ac.getBean("sayHelloServiceClient");

????????????? System.out.println(s.say("dfdsf"));

?????? }

}

(5)运行App程序如果出现下面错误

java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader,but this RI ***needs 2.1 API.

解决办法:
??? 1、如果是application工程,则在程序中打印出

???????? system.out.println(System.getProperty("java.endorsed.dirs"));
????????
找到相应位置,将jaxb-api-2.1.jarjaxb-impl-2.1.12.jar放到对应目录下即可。没有endorsed目录,自己建一个。
??? 2、如果是java web项目,则在一个servlet之类的运行起来的项目中,打印:

???????? system.out.println(System.getProperty("java.endorsed.dirs"));
????????
找到相应位置,将jaxb-api-2.1.jarjaxb-impl-2.1.12.jar放到对应目录下即可。没有endorsed目录,自己建一个。
???????? 我的WEB项目中,打印出来的路径是:E:Javaapache-tomcat-7.0.19endorsed

endorsed目录里面放置的jar文件,将有覆盖系统API的功能。可以牵强的理解为,将自己修改后的API打入到虚,拟机指定的启动API中,取而代之。

(编辑:李大同)

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

    推荐文章
      热点阅读