导入jar包cxf的jar包

创建实体类

package com.yhd.webservice.cxf.server.poto;

public class Person {

private String name;
private int age;

public Person() {
}

public Person(String name, int age) {
this.name = name;
this.age = 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;
}

}

创建接口

package com.yhd.webservice.cxf.server.service;

import java.util.List;

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

import com.yhd.webservice.cxf.server.poto.Person;

@WebService
public interface PersonService {

public List<Person> findAll(@WebParam(name = "arg0") String name);

}

创建实现类

package com.yhd.webservice.cxf.server.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import com.yhd.webservice.cxf.server.poto.Person;
import com.yhd.webservice.cxf.server.service.PersonService;

@WebService(endpointInterface="com.yhd.webservice.cxf.server.service.PersonService",serviceName="person")
public class PersonServiceImp implements PersonService {

@Override
public List<Person> findAll(String name){
ArrayList<Person> persons = new ArrayList<Person>();

Person p1 = new Person();
p1.setName(name + "3");
p1.setAge(18);

Person p2 = new Person();
p2.setName(name + "4");
p2.setAge(20);

persons.add(p1);
persons.add(p2);

return persons;
}
}

创建配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
xmlns="http://www.springframework.org/schema/beans">

<bean id="jaxWsServiceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
<property name="wrapped" value="true" />
</bean>

<jaxws:endpoint id="serviceimp" address="/person"
implementor="com.yhd.webservice.cxf.server.service.impl.PersonServiceImp">

<jaxws:serviceFactory>
<ref bean="jaxWsServiceFactoryBean" />
</jaxws:serviceFactory>
</jaxws:endpoint>
</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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WebServiceServer</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>

<display-name>cxfTest</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.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>/hello/*</url-pattern>
</servlet-mapping>

</web-app>

测试服务有没有发布成功  http://localhost:8080/cxf_spring/hello   服务地址

wsdl2java -d .      ?wsdl生成客户端调用代码

测试类

package com.yhd.webservice.cxf.client.main;

import java.util.List;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import per.Person;
import per.PersonService;

public class ClientTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(PersonService.class);
factory.setAddress("http://localhost:8080/cxf_spring/hello/person");
PersonService service = (PersonService)factory.create();
List<Person> list = (List<Person>)service.findAll("张");
for (Person person : list) {
System.out.println("name=" + person.getName());
System.out.println("age=" + person.getAge());
System.out.println("-------------------");
}
}

}

cxf整合spring代码的更多相关文章

  1. webservice 服务端例子+客户端例子+CXF整合spring服务端测试+生成wsdl文件 +cxf客户端代码自动生成

    首先到CXF官网及spring官网下载相关jar架包,这个不多说.webservice是干嘛用的也不多说. 入门例子 模拟新增一个用户,并返回新增结果,成功还是失败. 大概的目录如上,很简单. Res ...

  2. 【Java EE 学习 81】【CXF框架】【CXF整合Spring】

    一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一 ...

  3. WebService—CXF整合Spring实现接口发布和调用过程

    一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...

  4. CXF整合Spring之JaxWsProxyFactoryBean调用

    1.见解 1.1 客户端的接口代码还一定要和服务端的接口代码一样,连注解都要一样,不够灵活 1.2 当客户端访问服务器的请求地址时,如果服务端没有对应的地址,就会报错,但是又没有cxf的异常捕获处理 ...

  5. 【WebService】——CXF整合Spring

    相关博客: [WebService]--入门实例 [WebService]--SOAP.WSDL和UDDI 前言: 之前的几篇博客基本上都是使用jdk来实现WebService的调用,没有使用任何框架 ...

  6. CXF整合Spring开发WebService

    刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...

  7. cxf整合spring错误为:cvc-complex-type.2.4.c

    cxf整合spring,报错信息如下: Multiple annotations found at this line:- cvc-complex-type.2.4.c: The matching w ...

  8. cxf整合spring中出现的错误

    Caused by: java.lang.ClassNotFoundException: javax.wsdl.extensions.ElementExtensible at org.apache.c ...

  9. CXF整合Spring发布WebService实例

    一.说明: 上一篇简单介绍了CXF以及如何使用CXF来发布一个简单的WebService服务,并且介绍了客户端的调用. 这一篇介绍如何使用CXF与spring在Web项目中来发布WebService服 ...

随机推荐

  1. 使用Matplotlib

    1.Matplotlib是python的一个绘图库. 2.from matplotlib import pyplot as plt

  2. POJ 1860:Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22648   Accepted: 818 ...

  3. Vue.js——5.生命周期

    Vue的生命周期 创建阶段new Vue1,beforeCreate() 表示在实例没有被创建出来之前会执行它加载data和methods2,caeated() data 和methods被初始化了 ...

  4. h5-提升移动端的响应区域

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. ServletContext实现网站计数器

    在网站开发中,有很多功能需要使用ServletContext,比如: 1.网站计数器 2.网站在线用户的显示 3.简单的聊天系统 总之,如果是涉及到不用用户共享数据,而这些数据量不大,同时又不希望写入 ...

  6. SDL实践:产品经理如何驱动产品安全建设

    一.序言 本文从产品经理的角度出发,对产品经理的安全职责.产品驱动安全的内涵.工作内容.工作方法.所需安全资源.以及产品经理的安全工作量进行了分析.希望所有产品经理在没有心理负担的情况下,有目标.有方 ...

  7. 使用flask_sqlalchemy操作mysql的一个测试

    示例代码 from flask_sqlalchemy import SQLAlchemy from flask import Flask app=Flask(__name__) app.config[ ...

  8. 一、早期(Early Stage)

    一.早期(Early Stage) 如果单纯从零基础开始,早期(Early Stage)应该是一到两个月(由于英语与中文差异比与其他语言大,中国同学至少两个月,但也不应过长.我们的经验是一般中国同学会 ...

  9. github新手使用教程

    1.首先打开https://github.com/官网 注册一个github账号 2.注册成功之后,登录账号,创建一个属于自己的库 3.创建完成之后,为了方便电脑上的代码上传到github 仓库上,要 ...

  10. Vue 项目中应用

    Vue使用 一.vue生命周期 # main.js import Vue from 'vue' import App from './App.vue' import router from './ro ...