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

WebService简单例子--cxf整合spring

发布时间:2020-12-17 00:21:23 所属栏目:安全 来源:网络整理
导读:CXF是目前最流行的webService框架,今天写了一个小demo,还是以HelloWorld开头,的源码在附件. 服务端:工程名取名为WebService,该下的包去官方下cxf包。 一、建一个实体类User和一个HelloWorld接口及 HelloWorld接口实现类 HelloWorldImpl ,包名是com.lyq
CXF是目前最流行的webService框架,今天写了一个小demo,还是以HelloWorld开头,的源码在附件.
服务端:工程名取名为WebService,该下的包去官方下cxf包。
一、建一个实体类User和一个HelloWorld接口及 HelloWorld接口实现类 HelloWorldImpl ,包名是com.lyqf:
User:
package com.lyqf;

public class User {
?? private String name;
?? private int age;

?? public String getName() {
???? return name;
??}

?? public void setName(String name) {
???? this.name = name;
??}

?? public int getAge() {
???? return age;
??}

?? public void setAge( int age) {
???? this.age = age;
??}

}
HelloWorld接口:
package com.lyqf;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
????String sayHi(String text);????
???? int getNum( int num);
????User getUsers();
}
HelloWorldImpl实现类:
package com.lyqf;

public class HelloWorldImpl implements HelloWorld{
public String sayHi(String text) {
// TODO Auto-generated method stub
return "Hi,"+text;
}
public int getNum(int num) {
return num;
public User getUsers() {
User user = new User();
user.setAge(13);
user.setName("李导");
return user;
}
二、web.xml文件:
<?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">
? <display-name></display-name>
? <welcome-file-list>
? ? <welcome-file>index.jsp</welcome-file>
? </welcome-file-list>
??
? <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>1</load-on-startup> ?
? ? </servlet> ?
??
? ? <servlet-mapping> ?
? ? ? ? <servlet-name>CXFServlet</servlet-name> ?
? ? ? ? <url-pattern>/*</url-pattern> ?
? ? </servlet-mapping>
</web-app>

三、在web-inf下面新建一个beans.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"> ?
??
? ? <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.lyqf.HelloWorldImpl" address="/HelloWorld" /> ?
</beans>


四、然后发布到tomcat上面: http://localhost:8080/WebService/HelloWorld?wsdl看结果。





客用端:新建一个web项目(java项目)把cxf包全导进来,
现在都cxf都自带wsdl2java工具,有两种方法来做:
1、编写bulid.xml文件,内容是:
<?xml version="1.0" encoding="UTF-8"?>
<project name="cxf wsdl2java" basedir="."> ??
? ?<property name="cxf.home" location ="${basedir}/WebRoot/WEB-INF/"/>
? ?<path id="cxf.classpath">
? ? ? <fileset dir="${cxf.home}/lib">
? ? ? ? ?<include name="*.jar"/>
? ? ? </fileset>
? ?</path> ? ? ?
? ?<target name="cxfWSDLToJava">
? ? ? <java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true">
? ? ? ? ?<arg value="-client"/>
? ? ? ? ?<arg value="-d"/>
? ? ? ? ?<arg value="src"/>
? ? ? ? ?<arg value="http://localhost:8080/WebService/HelloWorld?wsdl"/>
? ? ? ? ?<classpath>
? ? ? ? ? ? <path refid="cxf.classpath"/>
? ? ? ? ?</classpath>
? ? ? </java>
? ?</target>
</project>
注:小白可能不懂,其实直接在bulid.xml右键ant就就可以执行了,然后F5刷新一下,src下面就有很多java代码了。
2、通过配置像JAVA_HOME来做,比如取名叫:JAVA_CXF,然后找到他的位置,再通过path找到cxf的bin目录。网上很多教程。

新建一个Client类,这个就是客户端的测试:
package com.lyqf;

public class Client {
public static void main(String[] args) {
HelloWorldImplService fatory = new HelloWorldImplService();
//这里是返回的是接口代理
HelloWorld hw = fatory.getHelloWorldImplPort();
User us = hw.getUsers();
System.out.println(us.getName()+" ?"+us.getAge());
}
}
输出看下结果。

所用的lib:http://down.51cto.com/data/779187

(编辑:李大同)

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

    推荐文章
      热点阅读