http://cxf.apache.org/docs/a-simple-jax-ws-service.html
This example will lead you through creating your first service with doing "code first" development with JAX-WS.
This example corresponds to the hello_world_code_first example in the CXF distribution. IMPORTANT : This sample is only in CXF 2.0.1+!
Setting up your build
If you're using Maven to build your project have a look at this page .
Otherwise, open up your favorite IDE and create a new project. The first thing we need to do is add the necessary CXF dependencies to the project. You can find these dependencies in the CXF distribution in the lib directory. (note: the version numbers on these jars may be different if versions have changed)
commons-logging-1.1.1.jar geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar) geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250) geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar) geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar) geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181) geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar) geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar) jaxb-api-2.1.jar jaxb-impl-2.1.12.jar jetty-6.1.21.jar jetty-util-6.1.21.jar neethi-2.0.4.jar saaj-api-1.3.jar saaj-impl-1.3.2.jar wsdl4j-1.6.2.jar wstx-asl-3.2.8.jar XmlSchema-1.4.5.jar xml-resolver-1.2.jar
The Spring jars (optional - for XML Configuration support):
aopalliance-1.0.jar spring-core-2.5.5.jar spring-beans-2.5.5.jar spring-context-2.5.5.jar spring-web-2.5.5.jar
And the CXF jar:
cxf-2.2.3.jar
Writing your Service
First we'll write our service interface. It will have one operation called "sayHello" which says "Hello" to whoever submits their name.
@WebService public interface HelloWorld { String sayHi(String text); /* Advanced usecase of passing an Interface in. JAX-WS/JAXB does not * support interfaces directly. Special XmlAdapter classes need to * be written to handle them */ String sayHiToUser(User user); /* Map passing * JAXB also does not support Maps. It handles Lists great, but Maps are * not supported directly. They also require use of a XmlAdapter to map * the maps into beans that JAXB can use. */ @XmlJavaTypeAdapter(IntegerUserMapAdapter.class) Map<Integer , User> getUsers(); }
To make sure your parameter is named correctly in the xml you should use:
@WebService public interface HelloWorld { String sayHi(@WebParam(name="text" ) String text); }
The @WebParam annotation is necessary as java interfaces do not store the Parameter name in the .class file. So if you leave out the annotation your parameter will be named arg0.
Our implementation will then look like this:
package demo.hw.server; import java.util.LinkedHashMap; import java.util.Map; import javax.jws.WebService; @WebService(endpointInterface = "demo.hw.server.HelloWorld" , serviceName = "HelloWorld" ) public class HelloWorldImpl implements HelloWorld { Map<Integer , User> users = new LinkedHashMap<Integer , User>(); public String sayHi(String text) { System .out.println("sayHi called" ); return "Hello " + text; } public String sayHiToUser(User user) { System .out.println("sayHiToUser called" ); users.put(users.size() + 1, user); return "Hello " + user.getName(); } public Map<Integer , User> getUsers() { System .out.println("getUsers called" ); return users; } }
The @WebService annotation on the implementation class lets CXF know which interface we want to create our WSDL with. In this case its simply our HelloWorld interface.
Publishing your service
System .out.println("Starting Server" ); HelloWorldImpl implementor = new HelloWorldImpl(); String address = "http://localhost:9000/helloWorld" ; Endpoint.publish(address, implementor);
Alternatively you can use the follwing code. This gives you more control over the behaviour. For example you can add a logging interceptor:
HelloWorldImpl implementor = new HelloWorldImpl(); 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();
You could leave out the ServiceClass. But it is better to use it so the server and the client are created from the same interface. If you instead only use the implementation class subtle problems may occur.
Pointing your browser at http://localhost:9000/helloWorld?wsdl will display the wsdl for this service
Accessing your service
and client code to see it working is at http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/demo/hw/client/Client.java
For the client there is also the alternative approach that gives you more flexibility. Of course like above the logging interceptors are optional but they help a lot when starting:
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); System .exit(0);
相关推荐
2. **JAX-WS**(Java API for XML Web Services)是Java平台上的SOAP(Simple Object Access Protocol)Web服务标准,主要用于创建面向服务的架构(SOA)。JAX-WS提供了处理XML消息、WSDL(Web服务描述语言)和UDDI...
在Java环境中,JAX-WS提供了一套完整的工具和API,使得开发人员能够无缝地实现SOAP(Simple Object Access Protocol)服务。 标题提到的"JAX-WS所需Jar包"是指为了在Java环境中支持JAX-WS功能,开发者需要的一组...
JAX-WS(Java API for XML Web Services)是Java平台标准版(Java SE)和企业版(Java EE)的一部分,它为创建、部署和消费基于SOAP(Simple Object Access Protocol)的Web服务提供了全面的支持。JAX-WS允许开发者...
JAX-WS 2.0是JAX-WS的第二个主要版本,它在JAX-RPC(Java API for XML-based RPC)的基础上进行了改进,引入了许多新特性以提升开发者的体验和效率。 **JAX-WS 2.0 的核心概念:** 1. **服务端点接口(SEI, ...
JAX-WS,全称为Java API for XML Web Services,是Java平台上的一个标准,用于构建和部署基于SOAP(Simple Object Access Protocol)的Web服务。它是Java SE和Java EE平台的一部分,简化了开发人员在Web服务领域的...
JAX-WS提供了一种简单的方式来创建SOAP(Simple Object Access Protocol)Web服务,它集成了Java SE和Java EE平台,使得开发者可以方便地实现服务接口和服务实现,然后通过工具自动生成WSDL(Web Service ...
**JAX-WS API** Java API for XML Web Services (JAX-WS) 是Java平台上的一个标准接口,用于创建和消费Web服务。它是Sun Microsystems在2004年推出的一个重要框架,旨在简化Web服务的开发,使得Java开发者能够更...
客户端则利用JAX-WS的`javax.xml.ws.Service`类来生成代理类,通过这个代理类调用远程服务。 【标签】"jax-ws"进一步确认了这个压缩包的内容主要围绕JAX-WS技术。JAX-WS在Java EE环境中广泛使用,特别是在EJB和Web...
JAX-WS是Java平台标准版(Java SE)和企业版(Java EE)的一部分,用于构建基于SOAP(Simple Object Access Protocol)的Web服务和客户端。这个压缩包可能包含了JAX-WS实现的核心库和其他相关组件。 **描述中的文件...
总结来说,学习JAX-WSWebService开发意味着掌握Web服务的基本概念、JAX-WS的API用法,以及在MyEclipse这样的IDE中实现和调试Web服务的方法。通过这个过程,开发者可以熟练地创建、部署和调用Java平台上的Web服务,...
2. **生成WSDL**:JAX-WS会自动生成一个WSDL(Web Service Description Language)文件,描述服务的接口、消息格式和地址等信息。 3. **发布服务**:使用`Endpoint.publish()`方法将服务部署到指定的URL。 4. **...
Java API for XML Web Services(JAX-WS)是Java平台上的一个标准,用于构建和部署Web服务。它简化了Web服务的开发,使得开发者能够使用Java编程语言来创建、调用和部署SOAP(Simple Object Access Protocol)服务。...
**JAX-WS** 是一种用于构建和部署Web服务的标准API,它使用SOAP(Simple Object Access Protocol)协议进行通信。通过JAX-WS,开发者可以方便地创建服务端点(SEI,Service Endpoint Interface)并将其暴露为Web服务...
在本篇文章中,我们将探讨如何利用Java SDK中的JAX-WS(Java API for XML Web Services)来创建和消费Web服务。 JAX-WS是Java平台上的一个标准组件,用于构建和部署SOAP Web服务。它简化了Web服务开发过程,提供了...
JAX-WS使用WSDL(Web Service Description Language)来定义服务接口,SOAP(Simple Object Access Protocol)作为消息传输协议,以及XML Schema来描述数据类型。通过JAX-WS,开发者可以轻松地创建服务端点(SEI,...
Java API for XML Web Services (JAX-WS) 是Java平台上用于构建Web服务和客户端的API。这个"jax-ws WebService客户端与服务端工程.rar"压缩包文件包含了一个完整的示例,展示了如何使用JAX-WS创建和消费Web服务。让...
【Java WebService Jax-WS 创建】是Java中创建Web服务的一种常见方式,主要用于构建基于SOAP(Simple Object Access Protocol)的Web服务。JAX-WS(Java API for XML Web Services)是Java EE 5及更高版本中提供的...
1. **生成WSDL(Web Service Description Language)文件**:JAX-WS工具如`wsimport`可以自动从服务接口生成WSDL文件,描述服务的接口和消息格式。 2. **创建部署描述符(web.xml)**:在`WEB-INF`目录下创建或更新`...
JAX-WS是SOAP(Simple Object Access Protocol)消息处理的Java规范,支持WSDL(Web Service Description Language)来定义服务接口。 在这个"jax-ws例子"中,我们有两个主要的组成部分:服务端项目和客户端项目。 ...
- **服务提供者接口(Service Provider Interface, SPI)**:JAX-WS的SPI允许开发者通过实现特定接口来定义服务端点,这些接口提供了处理HTTP请求和响应的能力。 - **SOAP消息处理**:JAX-WS支持SOAP 1.1和1.2协议...