cxf的hello world
1.建立一个maven工程,dom.xml。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lzm.cxf</groupId> <artifactId>cxf</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <cxf.version>3.0.1</cxf.version> <spring.version>3.2.10.RELEASE</spring.version> <finalName>cxf.war</finalName> </properties> <dependencies> <!-- <dependency> <groupId>org.apache.cxf</groupId> <artifactId>apache-cxf</artifactId><version>3.0.1</version> <type>pom</type> </dependency> --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <!-- Jetty is needed if you're are not using the CXFServlet --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <!-- 设置war包名称 --> <finalName>${finalName}</finalName> <plugins> <!-- jetty自动部署插件 只支持jdk1.7--> <!-- scanIntervalSeconds设置的是jetty自动扫描的间隔,单位秒 发现有改动自动更新部署,默认为0,即不扫描管理热部署工程。 --> <!-- contextPath配置的是部署后文件夹的名字,也即访问的地址。 例:http://localhost:8080/autoJetty --> <!-- Important: You need to use Maven 3 and Java 1.7 for this plugin. --> <!-- <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.1.v20120215</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webAppConfig> <contextPath>/</contextPath> </webAppConfig> </configuration> </plugin> --> </plugins> </build> </project>
2.cxf可以用2种方式跑,一是用main,一是用web 容器(tomcat)。
如果是用main。
必须保证dom.xml里有以下配置
<!-- Jetty is needed if you're are not using the CXFServlet --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency>
Server.java
package com.lzm.cxf; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.lzm.cxf.service.HelloWorld; import com.lzm.cxf.service.imp.HelloWorldImpl; public class Server { protected Server() throws Exception { /** * simple */ /* System.out.println("Starting Server"); HelloWorldImpl implementor = new HelloWorldImpl(); String address = "http://localhost:9000/helloWorld"; Endpoint.publish(address, implementor);*/ /** * complex */ HelloWorldImpl implementor = new HelloWorldImpl(); int i = 0; JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean(); svrFactory.setServiceClass(HelloWorld.class); svrFactory.setAddress("http://localhost:9000/helloWorld"); svrFactory.setServiceBean(implementor); svrFactory.getInInterceptors().add(new LoggingInInterceptor()); svrFactory.getOutInterceptors().add(new LoggingOutInterceptor()); svrFactory.create(); } public static void main(String args[]) throws Exception { new Server(); System.out.println("Server ready..."); System.in.read(); } }
Client.java
package com.lzm.cxf; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.lzm.cxf.service.Address; import com.lzm.cxf.service.HelloWorld; public class Client { private Client() { } public static void main(String args[]) throws Exception { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.getInInterceptors().add(new LoggingInInterceptor()); factory.getOutInterceptors().add(new LoggingOutInterceptor()); factory.setServiceClass(HelloWorld.class); factory.setAddress("http://localhost:9000/helloWorld"); HelloWorld client = (HelloWorld) factory.create(); String reply = client.sayHi("HI"); System.out.println("Server said: " + reply); Address address = client.getAddress(1l); System.out.println(address.getCity()); } }
package com.lzm.cxf.service; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService public interface HelloWorld { @WebResult(name="result") public String sayHi(@WebParam(name="text") String text); @WebResult(name="result") public Address getAddress(@WebParam(name="id") long id); }
package com.lzm.cxf.service.imp; import java.util.Arrays; import javax.jws.WebService; import com.lzm.cxf.service.Address; import com.lzm.cxf.service.HelloWorld; @WebService(endpointInterface = "com.lzm.cxf.service.HelloWorld", serviceName = "HelloWorld") public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { System.out.println("sayHi called"); return "Hello " + text; } public Address getAddress(long id) { // TODO Auto-generated method stub Address address = new Address(); address.setCity("city"); address.setCounty("country"); address.setId(1); address.setPhoneNumbers(Arrays.asList(new String[] {"1234567", "2222299"})); return address; } }
package com.lzm.cxf.service; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlType; /** * * @author bragel * */ @XmlType @XmlAccessorType(XmlAccessType.FIELD) public class Address { @XmlElement(name = "address_id") private int id; @XmlElement(name = "street") private String street; @XmlElement(name = "suite") private String suite; @XmlElement(name = "county") private String county; @XmlElement(name = "city") private String city; @XmlElement(name = "state") private String state; @XmlElement(name = "zip") private String zip; @XmlElementWrapper(name = "phone_numbers") @XmlElement(name = "phone_number") private List<String> phoneNumbers; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getSuite() { return suite; } public void setSuite(String suite) { this.suite = suite; } public String getCounty() { return county; } public void setCounty(String county) { this.county = county; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } public List<String> getPhoneNumbers() { return phoneNumbers; } public void setPhoneNumbers(List<String> phoneNumbers) { this.phoneNumbers = phoneNumbers; } }
3.如果是用tomcat跑
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>cxf</display-name> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
WEB-INF/cxf-servlet.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-servlet.xml" /> <bean id="hello" class="com.lzm.cxf.service.imp.HelloWorldImpl" /> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> </beans>
src/main/resources/client-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/schema/jaxws.xsd"> <bean id="client" class="com.lzm.cxf.service.HelloWorld" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.lzm.cxf.service.HelloWorld" /> <property name="address" value="http://localhost:9002/services/HelloWorld" /> </bean> </beans>
4.用soapUI 4.5.1测试。
WSDL填入http://localhost:9000/helloWorld?wsdl
相关推荐
【CXF笔记】是关于Apache CXF框架的深入学习记录,该笔记可能涵盖了CXF的基本概念、使用方法、源码解析以及与工具的结合应用。Apache CXF是一个开源的Java框架,主要用于构建和开发Web服务。它允许开发者通过Java...
在本文中,我们将探讨如何将Apache CXF 3.0.2版本集成到Spring框架中,以及在这个过程中可能遇到的问题和解决方案。Apache CXF是一个开源的SOA(Service-Oriented Architecture)框架,它允许开发人员创建和消费各种...
在了解如何使用Eclipse和CXF开发和部署Web服务之前,我们需要先明确一些关键的基础概念。 首先,CXF是一个开源的服务框架,它支持创建Web服务,特别是基于SOAP和REST的Web服务。它提供了丰富的工具来快速开发和部署...
当我们谈论“Axis2,CXF版本学习笔记”时,这通常涉及到两个主要的Java Web服务框架:Apache Axis2和Apache CXF。这两个框架都用于构建和部署SOAP(简单对象访问协议)和RESTful(Representational State Transfer)...
【WebService的实现——CXF学习笔记】 在Java世界中,WebService是一种通过标准协议(如SOAP)进行跨平台、跨语言通信的技术。CXF是Apache软件基金会的一个开源项目,它提供了构建和消费Web服务的工具和库。CXF支持...
本学习笔记旨在提供对Apache CXF的基本理解、功能特性和实际操作指导。 **1. CXF 简介** 1.1 CXF概述 Apache CXF是一个全面的服务开发框架,它允许开发者创建和部署各种类型的Web服务,包括基于SOAP、RESTful、WS-...
总的来说,这份“webservice+cxf基础笔记和视频”资源将引导你进入Web服务的世界,通过学习和实践,你可以掌握使用CXF和Spring进行Web服务开发的基本技能。无论你是初学者还是有一定经验的开发者,这都将是一份有...
CXF(CXF: Composite eXtensible Framework)是一个开源的Java框架,它主要用于构建和服务导向架构(Service-Oriented Architecture, SOA)中的Web服务。CXF允许开发者通过多种方式来创建和消费Web服务,无论是基于...
【CXF Webservice初学笔记】 在IT行业中,Web服务是一种允许不同系统之间进行通信和交换数据的方法。Apache CXF是一个流行的开源框架,用于构建和部署Web服务。本笔记将探讨CXF Webservice的基础知识,包括其核心...