先创建 webservice 服务端 。 首先下载 cxf jar 包 , cxf-2.7.8 .

新建 web 项目 aa . 将下载的cxf 压缩文件解压,将lib 下的jar 全部build path 到项目下 , 如果 启动项目时 报 找不到 org.springframework.web.context.ContextLoaderListener

请将 所有的jar 包 直接拷贝到项目的lib 下。1. 新建接口  HappyNewYear,, 注意注解必须写@webservice,  其中参数@webresult 和 @webparam 在 客户端 写接口 时 ,这个方法也必须要有同样的 参数,否则报错找不到该方法.  客户端和服务端接口的包名要一致。

package com.service;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; import com.bean.Person;
@WebService
public interface HappyNewYear {
public @WebResult(name = "Greet") String sayHello(@WebParam(name = "friend") String person);
}

2.新建实现类HappyNewYearImpl  注意注解 和 endpointInterface

package com.service;

import javax.jws.WebService;

import com.bean.Person;

@WebService(endpointInterface = "com.service.HappyNewYear")
public class HappyNewYearImpl implements HappyNewYear
{ public String sayHello(String person)
{
return "Hello, " +person;
} }

3.web.xml文件配置

  

<?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>aa</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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/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>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

4. applicationContext.xml 文件配置,,注意 该文件的路径 和 导入 下面的三个resource 文件。调用 地址 为greetService

<?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="greetServicce" implementor="com.service.HappyNewYearImpl" address="/greetServicce" />
</beans>

访问http://localhost:8080/aa/greetServicce?wsdl  看是否出现你想要的接口 。

二 、客户端代码 。

创建 web 项目test  , 同样导入上面的jar包。

CXF 提供了两种创建客户端的方式,一种是使用wsdl2java 命令生成客户端 ,另一种就是动态创建客户端 、

1. 动态调用 时 涉及参数问题:

//1. 普通的java 语言客户端

/*
* cxf 客户端动态调用 ,,基础类型 直接传入 ,复杂类型 使用这种方式传入
* */


public class Text {
public static void main(String[] args) throws Exception{ /* //创建WebService客户端代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//注册WebService接口
factory.setServiceClass(HappyNewYear.class);
//设置WebService地址
factory.setAddress("http://localhost:8082/aa/greetServicce");
HappyNewYear greetingService = (HappyNewYear)factory.create();
Person person=new Person();
person.setFirstName("张三");
person.setLastName("李四");
System.out.println("message context is:"+greetingService.sayHello(person));
*/ // 注意这个Person 是 服务端 类 ,而路径对应的不是 服务端项目的路径 ,而是service,,不懂为什么。 JaxWsDynamicClientFactory clientFactory=JaxWsDynamicClientFactory.newInstance();
Client client=clientFactory.createClient("http://localhost:8082/aa/greetServicce?wsdl"); Object orderlist=Thread.currentThread().getContextClassLoader().loadClass("com.service.Person").newInstance();
Method setFirstName=orderlist.getClass().getMethod("setFirstName", String.class);
setFirstName.invoke(orderlist, "张三"); Method setLastName=orderlist.getClass().getMethod("setLastName", String.class);
setLastName.invoke(orderlist, "李四"); Object[] result=client.invoke("sayHello",orderlist);
System.out.println(result[0]); }
}

2. 第二种调用方式

1. 创建客户端接口

  

package com.service;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; /*
* spring cxf
*/
@WebService
public interface HelloWorld {
//String sayHello(String text);
//注意把 注解也要带过来
public @WebResult(name = "Greet") String sayHello(@WebParam(name = "friend") String person);
}

    2. 调用

  

package com.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloWorldClient {
public static void main(String[] args){
//private static Logger logger=Logger.getLogger(HelloWorldClient.class);
ApplicationContext context=new ClassPathXmlApplicationContext("application-client.xml");
HelloWorld client=(HelloWorld)context.getBean("client",HelloWorld.class);
System.out.println(client.sayHello("张三"));
}
}

3 . application-client.xml配置,同样 放在src 根目录下。

    

<?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" /> <!-- 主要bean和接口的路径一定要和 服务端一致 --> <bean id="client" class="com.service.HelloWorld" factory-bean="clientFactory" factory-method="create"></bean>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.service.HelloWorld"></property>
<property name="address" value="http://localhost:8082/aa/greetServicce"></property>
</bean>
</beans>

初学 ,按照网上来的。能够运行出来,原理什么的还是不懂啊,有高手推荐下学习 资料吗?

rest 风格:

可能需要手动添加jsr331-api-1.1.1.jar。。

接口:

package com.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; @Path(value="/sample")
public interface RestSimple {
@GET
@Path("/bean/{name}")
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public String getuser(@PathParam("name")String name);
}

  实现类,注意注解要保持一致。

package com.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; /**
* rest 风格webservice
* @author Administrator
*
*/
@Path(value="/sample")
public class HelloLove implements RestSimple{ @Override
@GET
@Path("/bean/{name}")
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public String getuser(@PathParam("name")String name) {
return name;
} }

  

配置文件,和上面的类似,

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.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" />
<bean id="person" class="com.bean.Person"></bean>
<bean id="restSample" class="com.rest.HelloLove"></bean> 对应实现类
<context:component-scan base-package="com.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<jaxws:endpoint id="greetServicce" implementor="com.service.HappyNewYearImpl" address="/greetServicce" />
<jaxrs:server id="restServiceContainer" address="/rest">
<jaxrs:serviceBeans>
<ref bean="restSample"/>
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>
<jaxrs:languageMappings>
<entry key="en" value="en-gb"/>
</jaxrs:languageMappings>
</jaxrs:server>
</beans>

访问http://localhost:8082/aa/rest?_wadl

客户端applicationContext.xml 配置文件添加

<bean id="webrest" class="org.apache.cxf.jaxrs.client.WebClient" factory-method="create">
<constructor-arg type="java.lang.String" value="http://localhost:8082/aa/rest"></constructor-arg>
</bean>

测试1

package com.test;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /*
*
* rest 风格测试
*/
public class TestRestLove {
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("application-client.xml");
WebClient client=ctx.getBean("webrest",WebClient.class);
System.out.println("rest 风格"+client.path("sample/bean/章程").accept(MediaType.APPLICATION_XML).get(String.class));
}
}

测试2:

配置添加

<bean id="restSampleBean" class="org.apache.cxf.jaxrs.client.JAXRSClientFactory" factory-method="create">
<constructor-arg type="java.lang.String" value="http://localhost:8000/CXFWebService/rest/" />
<constructor-arg type="java.lang.Class" value="com.hoo.service.RESTSample" />
</bean>
ApplicationContext ctx = new ClassPathXmlApplicationContext("application-client.xml"); 
RESTSample sample = ctx.getBean("restSampleBean", RESTSample.class);
System.out.println(sample.getBean("张三"));

客户端整合 :Spring  struts

导入Spring 和struts  架包 ,和 struts2-spring-plugin.jar ,主要该架包要和struts 包对应,以防冲突

HelloAction

package com.action;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.service.HelloWorld;
import com.service.Person; public class HelloAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
@Autowired
private HelloWorld hell;//service 也需要setter/getter 方法
private String text;
public String execute(){
Person p=new Person();
p.setFirstName("张三");
p.setLastName("李四");
text=hell.sayHello(p);
return SUCCESS;
} public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
} public HelloWorld getHell() {
return hell;
} public void setHell(HelloWorld hell) {
this.hell = hell;
} }

HelloWorldiml

package com.service;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; public class HelloWorldIml implements HelloWorld{ @Override
public String sayHello(Person person) {
/*ApplicationContext context=new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
HelloWorld client=(HelloWorld)context.getBean("client",HelloWorld.class);
return client.sayHello(person);*/ //这种方式只能读指定的xml 文件,
//对import 的xml文件不起作用,bean 引用也不起作用。
//所以找不到client ServletContext servletContext =ServletActionContext.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
HelloWorld bean = (HelloWorld)wac.getBean("client");
return bean.sayHello(person); } }

application.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" /> <!-- 主要bean和接口的路径一定要和 服务端一致 --> <!-- <bean id="client" class="com.service.HelloWorld" factory-bean="clientFactory" factory-method="create"></bean>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.service.HelloWorld"></property>
<property name="address" value="http://localhost:8082/aa/greetServicce"></property>
</bean>
<bean id="webrest" class="org.apache.cxf.jaxrs.client.WebClient" factory-method="create">
<constructor-arg type="java.lang.String" value="http://localhost:8082/aa/rest"></constructor-arg>
</bean> -->
<import resource="classpath*:application-client.xml"/><!-- classpath 指在classes下的文件,只取第一个。加*表示所有都加载 -->
<!-- 所有src目录下的文件 编译后都在WEB-INF/classes 目录下 -->
<bean id="helloService" class="com.service.HelloWorldIml"></bean>
<bean id="helloAction" class="com.action.HelloAction" scope="prototype">
<property name="hell" ref="helloService"></property>
</bean>
</beans>

web.xml

<?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">
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/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>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping> -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,classpath*:application-client.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:application-client.xml,
applicationContext.xml
</param-value>
</context-param> --><!-- 引入多个Spring 配置文件 --> </web-app>

jsp

<?xml version="1.0" encoding="utf-8" ?>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<font style="color:blue;">${text}</font>
</body>
</html>

struts-xml

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

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <!-- 必须引入2.0。不能是其他版本 -->
<!-- struts spring 整合需要各自架包 和 struts2-spring-plugin.jar -->
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.objectFactory" value="spring" />
<package name="gen" namespace="/" extends="struts-default">
<global-results>
<result name="error">error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
</package>
<package name="hello" namespace="/hello" extends="struts-default">
<action name="helloAction" class="helloAction">
<result>/hello.jsp</result>
</action>
</package>
</struts>

路径问题很常见,我也不是很明白,只能尝试。

2014.1.4 cxf spring webservice的更多相关文章

  1. 使用CXF+Spring发布WebService,启动报错

    使用CXF+Spring发布WebService,启动报错,日志如下: 五月 12, 2017 9:01:37 下午 org.apache.tomcat.util.digester.SetProper ...

  2. struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

    Cxf + Spring+ myeclipse+ cxf 进行  Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...

  3. 解决cxf+spring发布的webservice,types,portType和message以import方式导入

    用cxf+spring发布了webservice,发现生成的wsdl的types,message和portType都以import的方式导入的.. 原因:命名空间问题 我想要生成的wsdl在同个文件中 ...

  4. Spring Boot+CXF搭建WebService(转)

    概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...

  5. idea+maven+spring+cxf创建webservice应用(二)生成客户端程序

    idea+maven+spring+cxf创建webservice应用(二)生成客户端程序,以上一篇为基础"idea+maven+spring+cxf创建webservice应用" ...

  6. 使用Spring和Tomcat发布CXF SOAP WebService

    上一节中使用代理工厂JaxWsProxyFactoryBean来发布WebService, 这种方式必须指定运行的端口,如果端口被占用,就会发布失败. cxf的WebService也可利用Tomcat ...

  7. Spring Boot 使用 CXF 调用 WebService 服务

    上一张我们讲到 Spring Boot 开发 WebService 服务,本章研究基于 CXF 调用 WebService.另外本来想写一篇 xfire 作为 client 端来调用 webservi ...

  8. Spring集成CXF发布WebService并在客户端调用

    Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...

  9. CXF发布webService服务以及客户端调用

    这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...

随机推荐

  1. ANA网络分析

    ANN网络分析 Mnist手写数字识别 Mnist数据集可以从官网下载,网址: http://yann.lecun.com/exdb/mnist/ 下载下来的数据集被分成两部分:55000行的训练数据 ...

  2. sqoop1 与sqoop2的对比

    Sqoop是一款开源的工具,主要用于在Hadoop和传统的数据库(mysql.postgresql等)进行数据的传递,可以将一个关系型数据库(例如:MySQL.Oracle.Postgres等)中的数 ...

  3. leetcode171

    public class Solution { private int ConvertToC(char c) { ; switch (c) { case 'A': case 'a': rnt = ; ...

  4. Centos LVM 创建 删除 扩大 缩小

    新建LVM的过程1.使用fdisk 新建分区 修改ID为8e3.使用 pvcreate 创建 PV 4.使用 vgcreate 创建 VG 5.使用 lvcreate 创建 LV 6.格式化LV7.挂 ...

  5. 机器学习入门-Knn算法

    knn算法不需要进行训练, 耗时,适用于多标签分类情况 1. 将输入的单个测试数据与每一个训练数据依据特征做一个欧式距离. 2. 将求得的欧式距离进行降序排序,取前n_个 3. 计算这前n_个的y值的 ...

  6. 17 网络编程 C/S架构介绍

    1.什么是C/S架构 C指的是client(客户端软件),S指的是Server(服务器软件),本章的重点是教大家写一个C/S架构的软件,实现服务端软件与客户端软件基于网络通信. 2.计算机基础的知识- ...

  7. UI5-文档-4.33-Routing Back and History

    现在我们可以导航到细节页面并显示发票,但是还不能回到概览页面.我们将向细节页面添加一个back按钮,并实现一个函数,再次显示概述页面. Preview A back button is now dis ...

  8. jenkins 修改工作目录

    修改Jenkins路径 Jenkins的默认安装路径是/var/lib/jenkins 现在由于这个根目录的磁盘太小,所以切换到/data 目录下. Jenkins目录.端口.工作目录等信息在/etc ...

  9. Struts和Hibernate使用总结

    1   struts.xml重定向时报错    action cannot be found in the namespace/     http://blog.csdn.net/greetturin ...

  10. Virtualbox [The headers for the current running kernel were not found] (操作过程后还是失败,显示相同问题)

    在笔记本安装Ubuntu11.04增强功能失败 引用 fuliang@fuliang-VirtualBox:~$ sudo /etc/init.d/vboxadd setup Removing exi ...