Apache CXF + Spring:生成一个简单的客户端 [英] Apache CXF + Spring: Generating a Simple Client

查看:89
本文介绍了Apache CXF + Spring:生成一个简单的客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始用Spring学习Apache CXF了。首先,我尝试创建一个简单的客户端/服务器模型。

I've started learning the Apache CXF with Spring. First of all, I've tried to create a simple client/server model.

服务器端是:
service.HelloWorld.java

@WebService
public interface HelloWorld {
  String sayHi(String text);
}

service.HelloWorldImpl.java

@WebService(endpointInterface = "service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
   public String sayHi(String text) {
     return "Hello, " + text;
   }
}

客户端是:
client.Client.java
public class Client {

The client-side is: client.Client.java public class Client {

    public static void main(String[] args) {
          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new  String[] {"cxf-client-servlet.xml"});
          HelloWorld client = (HelloWorld) context.getBean("client");
          System.out.println(client.sayHi("Batman"));
    }
}

cxf-client-servlet.xml

<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-2.0.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schema/jaxws.xsd">

<bean id="client" class="service.HelloWorld" factory-bean="clientFactory" factory-method="create"/>

<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
    <property name="serviceClass" value="service.HelloWorld"/>
    <property name="address" value="http://localhost:8080/services/HelloWorld"/>
</bean>

问题是:制作客户工作我必须将service.HelloWorld(包+接口)添加到客户的项目中。我听说过在使用服务之前我需要生成一个存根。所以这对我来说很困惑。那么,什么是正确的方法,什么是最佳实践(可能最好使用一些契约优先方法或类似方法)?后来,我想添加WS-Security,所以我需要一个强大的背景=)

The problem is: to make the client work I've had to add service.HelloWorld (package + interface) to the clients's project. I've heard that before using a service I need to generate a stub. So it's confusing for me. So that, what is the correct approach and what is the best practice (may be it is better to use some contract-first approach or suchlike)? Later, I want to add WS-Security, so I need a strong background =)

提前致谢。

推荐答案

如果您正在进行代码优先的WS开发,那么分发接口并将其提供给客户端是可以的。 我认为接口(仅实现)上不需要 @WebService ,因此客户端不依赖于此注释。

If you are doing code-first WS development then it is fine to distribute the interface and give it to the client. I believe @WebService is not needed (?) on the interface (only implementation), so the client does not have dependencies on this annotation.

即使您正在执行代码优先的Web服务,您仍然可以下载Apache CXF为您生成的WSDL文档,并将其提供给客户端。使用这种方法(被认为更成熟,更不用说它可以在.NET等不同平台上使用),客户端必须生成存根(使用像 wsdl2java 这样的工具) 。这个过程实际上会自动创建非常相似的客户端界面。

Even if you are doing code-first web-services, you may still download the WSDL document generated for you by Apache CXF and give it to the client instead. With this approach (which is considered more mature, not to mention it can be used on different platforms like .NET) the client has to generate the stubs (using tool like wsdl2java). This process will essentially create very similar client interface automatically.

这就是为什么这么多人喜欢契约优先开发的原因之一 - 使用相同的WSDL来生成客户端 - side stubs和服务器端WS实现。这限制了(偶然)不兼容的范围。

That's one of the reasons why so many people prefer contract-first development - the same WSDL is used to generate client-side stubs and server-side WS implementation. This limits the scope of (accidental) incompatibilites.

这篇关于Apache CXF + Spring:生成一个简单的客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆