您的位置:首页 > 其它

[翻译]CXF用户指南:一个简单的JAX-WS服务

2011-08-03 15:02 330 查看
转自:http://www.iteye.com/topic/745233

原文地址:http://cxf.apache.org/docs/a-simple-jax-ws-service.html

这个例子将通过编写JAX-WS来引导你开发第一个服务

设置构建
编写服务
发布服务
访问服务

同样在CXF发布包中的也可以找到hello_world_code_first示例,注意:这个示例只有在CXF 2.0.1+的版本中!

设置构建

如果你使用Maven来构建你的工程,查看这个页面

否则,打开你最喜爱的IDE,然后创建一个新的工程,我们首先要做的是将CXF依赖的包添加到工程里,你可以在CXF的发布包的ib目录下找到这些依赖包(提示:如果发布包的版本改变,这些jar包上的版本数字可能有所不同)

引用
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

Spring依赖包 (非必须- 为了支持XML配置):

引用
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

还需要CXF的jar包:

引用
cxf-2.2.3.jar

编写服务

首先,我们将编写服务的接口,这个接口只有一个"sayHello" 的方法,这个方法会对提交他们名字的用户说 "Hello"

Java代码







@WebService
public interface HelloWorld {

String sayHi(String text);

/* 进阶应用,JAX-WS/JAXB不支持自定义类型参数如User

* 需要编写XmlAdapter类来处理自定义类型
*/
String sayHiToUser(User user);

/* Map传递
* JAXB同样也不支持Maps.处理Lists很轻松,但是不直接支持Maps

* 同样也需要使用XmlAdapter来映射maps到JAXB能处理的beans

*/
@XmlJavaTypeAdapter(IntegerUserMapAdapter.class)
Map<Integer, User> getUsers();
}

@WebService
public interface HelloWorld {

String sayHi(String text);

/* 进阶应用,JAX-WS/JAXB不支持自定义类型参数如User
* 需要编写XmlAdapter类来处理自定义类型
*/
String sayHiToUser(User user);

/* Map传递
* JAXB同样也不支持Maps.处理Lists很轻松,但是不直接支持Maps
* 同样也需要使用XmlAdapter来映射maps到JAXB能处理的beans
*/
@XmlJavaTypeAdapter(IntegerUserMapAdapter.class)
Map<Integer, User> getUsers();
}
为了确认参数在xml文件中名称正确,你需要使用:

Java代码







@WebService public interface HelloWorld { String sayHi(@WebParam(name="text") String text); }
@WebService
public interface HelloWorld {
String sayHi(@WebParam(name="text") String text);
}
因为java接口在.class文件中不存储参数名称,所以@WebParam注解是必须的,因此如果你不使用这个注解,参数将被命名为arg0

我们的实现将像这样:

Java代码







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;
}

}

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;
}

}
在实现类上的@WebService注解使CXF知道使用那个接口来创建WSDL,我们示例中是HelloWorld接口

发布服务

Java代码







System.out.println("Starting Server"); HelloWorldImpl implementor = new HelloWorldImpl(); String address = "http://localhost:9000/helloWorld"; Endpoint.publish(address, implementor);
System.out.println("Starting Server");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:9000/helloWorld";
Endpoint.publish(address, implementor);
完整的代码在http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/demo/hw/server/Server.java

或者你可以使用下列代码,通过如下代码,你可以更多控制行为,例如你可以添加日志拦截器

Java代码







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();
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();
你可以忽略设置ServiceClass,但是最好设置,这样可以使服务端和客户端都从同样的接口创建而来,如果你使用实现类来作为替代可能会出现问题

浏览器里输入http://localhost:9000/helloWorld?wsdl显示出这个服务的wsdl

访问服务

同样客户端代码在http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/demo/hw/client/Client.java

对于客户端,同样也有替代方法给予你更多的伸缩性,当然如上一个示例的日志拦截器也是可选择的,但是对于初学者帮助很大:

Java代码







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);

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: