java - Apache CXF + Spring : Generating a Simple Client

标签 java web-services spring cxf

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

服务器端是: 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.java 公共(public)类客户{

    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(包 + 接口(interface))添加到客户端的项目中。我听说在使用服务之前我需要生成一个 stub 。所以这让我感到困惑。那么,什么是正确的方法,什么是最佳实践(可能最好使用一些契约(Contract)优先的方法或类似方法)?后来想加入WS-Security,所以需要强大的背景 =)

提前致谢。

最佳答案

您可以在客户端使用像这样的简单 spring 配置 -

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

如果不需要,请忽略拦截器。

post. 中有更多详细信息

关于java - Apache CXF + Spring : Generating a Simple Client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9032960/

相关文章:

java - 具有重要名称的 WSDL 中的参数名称

c# - 设置超时异步 Web 服务调用?

java - Sitebricks 服务拦截?

java - Spring Boot 2 是否支持 Java 10?

java - 将转义的 Unicode 字符转换回实际字符

java - 对于可以在线和离线使用的应用程序,分离层的好策略是什么?

java - 我无法在代码中使用任何 ID

java - 不使用 JTextField 等绘制文本字段

Spring Cloud Gateway+Consul 配置

java - 如何获取传入请求 SOAP 的 IP 地址