我已经开始使用 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/