简体   繁体   English

Apache CXF + Spring:生成一个简单的客户端

[英]Apache CXF + Spring: Generating a Simple Client

I've started learning the Apache CXF with Spring. 我已经开始用Spring学习Apache CXF了。 First of all, I've tried to create a simple client/server model. 首先,我尝试创建一个简单的客户端/服务器模型。

The server-side is: service.HelloWorld.java 服务器端是: service.HelloWorld.java

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

service.HelloWorldImpl.java service.HelloWorldImpl.java

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

The client-side is: client.Client.java public class Client { 客户端是: 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 CXF的客户端- 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>

The problem is: to make the client work I've had to add service.HelloWorld (package + interface) to the clients's project. 问题是:为了使客户端工作,我必须将service.HelloWorld(包+接口)添加到客户端的项目中。 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-Security,所以我需要一个强大的背景=)

Thanks in advance. 提前致谢。

You can use a simple spring configuration like this for client side - 您可以为客户端使用这样的简单弹簧配置 -

<jaxws:client id="mywebServiceClient"
    serviceClass="com.saurzcode.TestService"
    address="http://saurzcode.com:8088/mockTestService">

    <jaxws:binding>
        <soap:soapBinding version="1.2" mtomEnabled="true" />
    </jaxws:binding>
</jaxws:client>
<cxf:bus>
    <cxf:outInterceptors>
        <bean class="com.saurzcode.ws.caller.SoapHeaderInterceptor" />
    </cxf:outInterceptors>
</cxf:bus>

Ignore the Interceptor if you don't need it. 如果你不需要它,请忽略拦截器。

More details in this post. 这篇文章中有更多细节

If you are doing code-first WS development then it is fine to distribute the interface and give it to the client. 如果您正在进行代码优先的WS开发,那么分发接口并将其提供给客户端是可以的。 I believe @WebService is not needed (?) on the interface (only implementation), so the client does not have dependencies on this annotation. 我认为接口(仅实现)上不需要@WebService (?),因此客户端不依赖于此注释。

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

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. 这就是为什么这么多人更喜欢契约优先开发的原因之一 - 相同的WSDL用于生成客户端存根和服务器端WS实现。 This limits the scope of (accidental) incompatibilites. 这限制了(偶然)不相容的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM