WebService server:
创建一个名为webserviceServer的web项目, 导入相关的jar包和CXF的jar包. CXF可以apache.org去下载.
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>webserviceServer</display-name>
<!-- 使用Spring Filter 配置字符集 -->
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 加载监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载环境上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/config/spring/spring-*.xml
</param-value>
</context-param>
<!-- Apache CXFServlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- CXFServlet Mapping -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring-context.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:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="userService" class="com.hao.webservice.service.impl.UserServiceImpl">
<property name="username" value="chenghao"/> <!--用户名-->
<property name="password" value="123456"/> <!--密码-->
</bean>
<!-- Expose UserWebService -->
<jaxws:server id="userWebService"
serviceClass="com.hao.webservice.service.IUserService"
address="/UserWebService"> <!--serviceClass是接口名, address是访问路径-->
<jaxws:serviceBean>
<ref bean="userService"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
IUserService.java:
package com.hao.webservice.service;
import javax.jws.WebService;
@WebService
public interface IUserService {
/**
* 登录
* @param username
* @param password
* @return
*/
String login(String username, String password);
}
UserServiceImpl.java:
package com.hao.webservice.service.impl;
import com.hao.webservice.service.IUserService;
public class UserServiceImpl implements IUserService {
private String username;
private String password;
@Override
public String login(String username, String password) {
if (username.equals(getUsername()) && password.equals(getPassword())) {
return "登录成功.";
} else {
return "登录失败.";
}
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
}
现在将webserviceServer项目发布到servlet容器里(tomcat,jetty). 可以在浏览器输入http://localhost:8080/webserviceServer进行查看.
WebService client:
创建一个名为webserviceClient的web项目或java项目, 导入相关的jar包和CXF的jar包. CXF可以apache.org去下载.
UserServiceTest.java
package com.hao.webservice.client.test;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import
public class UserServiceTest {
public static void main(String[] args) throws Exception {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/webserviceServer/UserWebService?wsdl");
//login 为接口中定义的方法名称 "chenghao","123456"为传递的2个参数 返回一个Object数组
Object[] objects=client.invoke("login", "chenghao","123456");
System.out.println(objects[0].toString());
}
}
然后运行UserServiceTest类.
分享到:
相关推荐
cxf+spring实现webservice
这里少了一个类,是根据实体类生成xml的文件下载地址为:http://download.csdn.net/detail/qq_14996421/9495688
cxf+spring+jdk1.5开发webService部署weblogic9.2
简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar...
CXF+Spring+Hibernate实现WebService,实现的方法比较简单,只有一个增加的方法,没有用到Struts 有服务端代码和客户端测试代码
CXF+Spring+自定义拦截器 webservice源码下载
CXF+Spring+Tomcat发布WebService
Weblogic92+CXF+XDoclet+Spring轻松搞定webservice 例子
CXF+spring+struts实现WebService
Spring+CXF+tomcat开发webservice,包含服务端和客户端,导入myeclipse就可以用。帮助大家搭建webservice框架,实现webservice功能。
cxf+spring实现webservice的例子,在eclipse开发环境上测试通过,由于上传限制,没有传lib,只把代码上传了,有需要的可以给我留言,我给单发!
Web项目中基于Maven与Spring整合的WebService之cxf的实现⬇️ 详情请参考如下链接: https://locqi.github.io/locqi.com/2018/09/05/Eclipse+Maven+Spring+CXF-create-WebService/
NULL 博文链接:https://chilongxph.iteye.com/blog/510707
cxf+spring+tomat开发webservice所需jar包,该jar包只需解压后导入web工程即可,亲测有用
本文主要运用当前比较流行的cxf+spring是想webservice的一个小的demo,在网上搜了许多的资料,最后经过自己的理解实现
基于CXF的WebService服务端和客户端示例,CXF+Spring,希望能为WebService开发者提供帮助。WebService,CXF,Spring.
这个是cxf 集成spring 的webservice的例子 供大家参考
SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验
cxf+spring开发webservice的简单实例,通过查看cfx的doc文档确定wsdl支持所引用的最少jar包
这个是cxf+spring发布webservice和restservice的服务端项目