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

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

服务器端是:
服务。HelloWorld.java

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

服务。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,所以我需要有很强的背景=)

提前致谢。

参考方案

如果您要进行代码优先的WS开发,则可以分发接口并将其提供给客户端。我相信接口(仅实现)上不需要@WebService(?),因此客户端不依赖于此批注。

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

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

Spring MVC中的输入验证 - java

我知道Commons Validator框架是Struts项目在服务器端和客户端验证输入值的事实上的标准。Spring MVC项目是否也是如此?我得到的印象可能不是,大多数Struts书籍和论坛都谈论Commons Validator框架,但是只有少数Spring书籍和论坛可以。在Spring MVC项目中验证输入的最佳实践是什么?干杯! 参考方案 在引入S…

Java:“自动装配”继承与依赖注入 - java

Improve this question 我通常以常见的简单形式使用Spring框架: 控制器服务存储库通常,我会在CommonService类中放一个通用服务,并使所有其他服务扩展到类中。一个开发人员告诉我,最好在每个服务中插入CommonClass而不是使用继承。我的问题是,有一个方法比另一个更好吗? JVM或性能是否会受到另一个影响?更新资料Comm…

在Java中,执行“ ++++++++”表达式,编译器未报告任何错误并且可以正确执行? - java

我用eclipse编写了这段代码,用war写过,结果为3d。public static void main(String[] args) { double a = 5d + + + + + +-+3d; System.out.println(a); } 参考方案 您的表情可以改写为(5d) + (+ + + + +-+3d) 其中第一个+是应用于两个操作数的…

Apache CXF客户端代理设置 - java

我正在尝试使用的教程来开发Soap Consumer for Soap Service。http://cxf.apache.org/docs/developing-a-consumer.html在“使用上下文设置连接属性”部分中,我正在看下面的代码// Set request context property. java.util.Map<String…

Hibernate通过非主键获取实体并更新它+ Spring MVC - java

当我使用其ID检索对象并更改其属性并更新它时,一切正常,但是当我使用其名称+版本获取对象并更新它们时,所有更改都不会保存在数据库中。您能不能让别人让我知道是什么问题?//通过id获取public PdfDocument get(Long id) { return (PdfDocument) session().get(PdfDocument.class, i…