简体   繁体   English

Java中的Web服务

[英]Web Services in Java

What do you usually use to connect to a Web Service when you are developing a Java project? 在开发Java项目时,您通常使用什么来连接Web服务?

There are different API-s that can do the job. 有不同的API可以完成这项工作。 From different books and tutorials I have read about: JAX-WS, JAXB, JAXM, JAXR, JAX-RPC, Axis ans so on. 从我读过的不同书籍和教程:JAX-WS,JAXB,JAXM,JAXR,JAX-RPC,Axis等等。

I'm interested in what exactly are you using and how much? 我对你究竟使用的是什么以及使用了多少感兴趣? Take this as a survey if you wish :) 如果你愿意,可以把这作为一项调查:)

To answer your question, we first need to differentiate between the tools you listed. 要回答您的问题,我们首先需要区分您列出的工具。

JAX-WS, JAXB, JAXM, JAXR, JAX-RPC are XML and Web service related APIs while Axis 1 and 2 are implementations of zero, one, or more of these APIs depending on the version. JAX-WS,JAXB,JAXM,JAXR,JAX-RPC是XML和Web服务相关的API,而Axis 1和2是这些API中的零个,一个或多个的实现,具体取决于版本。

JAX-B 1 and 2 are XML to object binding APIs, JAX-WS is a WSDL and SOAP based Web service API and the predecessor of JAX-RPC, JAX-M is an older XML messaging API and JAX-R is an abstraction API for interacting with registries such as UDDI and ebXML. JAX-B 1和2是XML到对象绑定API,JAX-WS是一个基于WSDL和SOAP的Web服务API,是JAX-RPC的前身,JAX-M是一个较旧的XML消息传递API,而JAX-R是一个抽象API用于与UDDI和ebXML等注册表进行交互。

From the Java.net JAX-RPC page: 从Java.net JAX-RPC页面:

The JAX-RPC expert group has wide industry participation with Sun Microsystems as the EG lead. JAX-RPC专家组在Sun Microsystems担任EG主管时拥有广泛的行业参与。 The initial specification (JAX-RPC 1.0) was JSR-101 and was released in June 2002. A maintenance release followed in October 2003 providing better integration with JAXB 1.0 as well as better support for doc/literal. 初始规范(JAX-RPC 1.0)是JSR-101,于2002年6月发布。2003年10月发布了维护版本,提供了与JAXB 1.0的更好集成以及对doc / literal的更好支持。

The next version of the spec was renamed from JAX-RPC 2.0 to JAX-WS 2.0 and is being developed as JSR-224; 规范的下一个版本从JAX-RPC 2.0重命名为JAX-WS 2.0,并且正在开发为JSR-224; this release will address a number of additional requirements in the area, and will increase the synergy between the JAXB and JAX-WS specifications. 此版本将解决该领域的许多其他要求,并将增加JAXB和JAX-WS规范之间的协同作用。 You can access the JAX-WS project page here. 您可以在此处访问JAX-WS项目页面。

Since SOAP stacks have come a long way since JAX-B 1.0 and JAX-RPC 1.0 I recommend staying far away from Axis 1.0 and XFire (which if I recall correctly doesn't even implement JAX-RPC 1). 自从JAX-B 1.0和JAX-RPC 1.0以来,SOAP堆已经走了很长的路,我建议远离Axis 1.0和XFire(如果我没记错的话,甚至不实现JAX-RPC 1)。 There are numerous SOAP stacks out there that implement the newer APIs (JAX-WS 2.x and JAX-B 2.x). 有许多SOAP堆栈可以实现更新的API(JAX-WS 2.x和JAX-B 2.x)。

As others have mentioned, Axis 2, JAX-WS RI, and CXF are all valid choices. 正如其他人所提到的,Axis 2,JAX-WS RI和CXF都是有效的选择。 These SOAP stacks are far more mature and support many modern WS-* specifications. 这些SOAP堆栈更加成熟,支持许多现代WS- *规范。

A word of caution about the comments regarding using your IDE to auto-generate client code. 关于使用IDE自动生成客户端代码的注释,请注意。 While I am a big proponent of generating XML data binding code and JAX-WS interfaces from XSDs and WSDLs, respectively, I caution using a built-in wizard in your IDE to perform the auto-generation. 虽然我是分别从XSD和WSDL生成XML数据绑定代码和JAX-WS接口的主要支持者,但我提醒您在IDE中使用内置向导来执行自动生成。 If you work on a team with more than one developer or plan to modify the generated code you should consider the maintainability of such an approach. 如果您在一个拥有多个开发人员的团队中工作或计划修改生成的代码,您应该考虑这种方法的可维护性。

If you have more than one developer, there will come a time when one of them is using a different version of the auto-generation tool, a different IDE, or has a different configuration in their tool. 如果您有多个开发人员,那么其中一个人将使用不同版本的自动生成工具,不同的IDE,或者在他们的工具中使用不同的配置。 Additionally, if you auto-generate from a wizard, it is up to the developers to remember how they generated the code in the event that you need to re-generate it in the future. 此外,如果您从向导自动生成,则由开发人员记住,如果您将来需要重新生成代码,他们将如何生成代码。 If you change the XSD and don't remember your configuration from the last time you auto-generated, the generated code may not align with the existing code that is already used throughout your program. 如果更改XSD并且不记得上次自动生成时的配置,则生成的代码可能与整个程序中已使用的现有代码不一致。

If you plan to modify the generated code, make sure that you only ever need to do it once and that from then on you are comfortable maintaining the code by hand or merging the regenerated code with your modifications on a regular basis. 如果您计划修改生成的代码,请确保您只需要执行一次,从那时起您可以轻松地手动维护代码或定期将重新生成的代码与修改合并。

Both of these issues can be avoided by scripting the code generation in your build process. 通过在构建过程中编写代码生成脚本可以避免这两个问题。 JAX-WS and JAX-B both come with Ant tasks and/or Maven 2 plug-ins that are easy to use in your builds. JAX-WS和JAX-B都带有Ant任务和/或Maven 2插件,这些插件易于在您的构建中使用。 Take these warnings seriously as I have seen multiple projects suffer through these issues when they needed to modify code that was generated 5 years ago by an employee that had since left the firm. 严肃对待这些警告,因为我看到多个项目在需要修改5年前由离开公司的员工生成的代码时会遇到这些问题。

My final words of caution are to be careful when allowing a tool to auto-generate your Web service interfaces from your WSDLs. 我最后提醒的是,在允许工具从WSDL中自动生成Web服务接口时要小心。 The JAX-WS RI WSDL2Java tool likes to place hard-coded paths to the WSDL in the generated interfaces. JAX-WS RI WSDL2Java工具喜欢将硬编码路径放置在生成的接口中的WSDL中。 It is my opinion that you should auto-generate the interfaces once and then remove the hard-coded URLs and QName references to make the interface applicable to all Web services that implement the WSDL Binding that the interface represents and not just the one endpoint that your WSDL describes. 我认为您应该自动生成一次接口,然后删除硬编码的URL和QName引用,使接口适用于实现接口所代表的WSDL绑定的所有Web服务,而不仅仅是您的一个端点。 WSDL描述。

you can use, apache axis. 你可以使用,apache轴。 This will generate the java stubs automatically if you provide the WSDL. 如果您提供WSDL,这将自动生成Java存根。 once the stubs are generated its just like calling a normal java class. 一旦生成了存根,就像调用普通的java类一样。

Axis proponents here need to be precise. 这里的轴支持者需要精确。

The Axis 1.x project was abandoned after Axis 1.4 was released in April 2006, over three years ago. 在Axis 1.4于2006年4月发布之后, Axis 1.x项目在三年前被放弃了。 We've recently encountered several very critical thread safety bugs in the Axis 1.4 client libraries, including 100% CPU spins and deadlocks. 我们最近在Axis 1.4客户端库中遇到了几个非常关键的线程安全漏洞,包括100%的CPU旋转和死锁。 These are well documented (and still unresolved) in the Axis 1.x bug database. 这些在Axis 1.x错误数据库中有详细记录(并且仍未解决)。 Needless to say we are giving up on Axis 1.x (and just using raw Apache HTTP client code). 不用说,我们放弃了Axis 1.x(并且只使用原始的Apache HTTP客户端代码)。

Axis 2 is an entirely new code base ... perhaps someone else can comment on it. Axis 2是一个全新的代码库......也许其他人可以评论它。

Based on our experience we'd consider Metro , CXF , hand-coding , and (maybe) Axis 2 for SOAP web services. 根据我们的经验,我们会考虑使用MetroCXF手工编码和(可能) Axis 2来实现SOAP Web服务。 (We recommend REST-based approaches over SOAP whenever you have a choice, and are using the Restlet framework, which we love) (我们建议您选择基于REST的方法而不是SOAP,并使用我们喜欢的Restlet框架)

IMO, you'd be absolutely nuts to go with Axis 1.x IMO,你绝对不会选择Axis 1.x

因为我们在Spring上投入了相当多的资金,所以我们将Spring-WS与JAXB一起使用。

我已经使用了Axis和Axis2,发现它们都非常好。

I think the most common use is with Apache Axis2. 我认为最常见的用途是使用Apache Axis2。 It's very easy to create services with it and you will find a lot of tutorials. 用它创建服务非常容易,你会发现很多教程。

I've used both JAX-WS RI and Apache CXF . 我已经使用了JAX-WS RIApache CXF If you are using Spring then CXF is a very good option. 如果您使用的是Spring,那么CXF是一个非常好的选择。 As Phill mentioned, there is Spring-WS as well, but CXF builds upon the JAX-WS spec. 正如Phill所提到的,还有Spring-WS ,但CXF建立在JAX-WS规范之上。 If you are not using Spring then I would say that the RI is the way to go, especially since it is bundled with Java 6. 如果您不使用Spring,那么我会说RI是要走的路,特别是因为它与Java 6捆绑在一起。

对于POX + HTTP或RESTful Web服务, Restlet或一个不错的HTTP客户端实现是完全足够的。

+1 for Apache Axis. Apache Axis为+1。

But JAX-WS would also be a good choice. 但JAX-WS也是一个不错的选择。

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

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