【发布时间】:2012-01-27 11:58:57
【问题描述】:
我已经开始使用 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.Client.java 公共类客户{
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,所以需要强大的背景=)
提前致谢。
【问题讨论】:
标签: java web-services spring cxf