java 如何用java编写有效的Web服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2810485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write effective web services in java
提问by gekrish
Though this might appear as a duplicate of Java Web Services, I would like to know Where to start and to continue.In the past, I have invested so much of time to find where to start but I wasn't able to. There are so many jargons and chaos (at least for me!) while reading the pages about web services. There are so many terms - like JAX-RPC, JAX-WS, Axis, Rest, Servlet as WebService, EJB'sas Web Service and other terms that I don't know. Can this User group consolidate and give a highlevel overview of Java Web Services which is easy to understand and follow? I appreciate your kindness and thanks for your help.
虽然这可能看起来像是Java Web Services的副本,但我想知道从哪里开始和继续。过去,我花了很多时间来寻找从哪里开始,但我没能做到。在阅读有关 Web 服务的页面时,有太多行话和混乱(至少对我而言!)。有很多术语——比如JAX-RPC, JAX-WS, Axis, Rest, Servlet as WebService, EJB'sWeb 服务和其他我不知道的术语。此用户组能否合并并提供易于理解和遵循的 Java Web 服务的高级概述?我很感激你的好意并感谢你的帮助。
回答by ewernli
That's indeed a bit a jungle to understand web services. The wikipediapage is decent, but still lacks some elements.
这确实有点难以理解 Web 服务。在维基百科页面是体面的,但还缺少一些元素。
I've flagged this answer as community wiki, so feel free to update it, or correct it. It's only a basis.
我已将此答案标记为社区维基,因此请随时更新或更正它。这只是一个基础。
A bloated term:
一个臃肿的术语:
First, the term web serviceis used to refer to many thing. While many people use it to refer to SOAP-based web service, the term can be used to denote any service provided through a web interface; this is a source of confusion.
首先,术语Web 服务用于指代许多事物。虽然很多人用它来指代基于 SOAP 的 Web 服务,但该术语可用于表示通过 Web 界面提供的任何服务;这是一个混乱的根源。
Implementation and design style:
实现和设计风格:
- SOAP-based-- SOAP is still the de-facto standard for web services. SOAP is protocol on top of HTTP that describes the exchange of message and exception. SOAP grew from something simple to something very complicated with all the WS-* standardsthat have been added later. The most important are: WS-Policy, WS-Security, WS-Addressing, WS-Transaction. Another important spec is MTOM for large message.
- RESTful-- The term RESTfulrelates to the fact that the service is stateless and all relevant information is passed as parameter. Also instead of using a protocol like SOAP, plain HTTP verbsare used, e.g.
Get,Put,Delete,Update. - Stateless-- WS are usually stateless. Business processed sometimes rely on so-called correlation identifiers (with WS-Addressing) that are used to match requests and response together; this is the same idea like storing a session identifier in a cookie because HTTP is stateless.
- Stateful-- There are some proposal to have stateful WS, but I don't know much about it.
- 基于SOAP——SOAP 仍然是 Web 服务的事实上的标准。SOAP 是 HTTP 之上的协议,它描述了消息和异常的交换。随着后来添加的所有WS-* 标准,SOAP 从简单的东西发展到非常复杂的东西。最重要的是:WS-Policy、WS-Security、WS-Addressing、WS-Transaction。另一个重要的规范是用于大消息的 MTOM。
- RESTful—— 术语RESTful与服务是无状态的并且所有相关信息都作为参数传递的事实有关。也不是使用像 SOAP 这样的协议,而是使用普通的 HTTP动词,例如
Get,Put,Delete,Update。 - 无状态——WS 通常是无状态的。处理的业务有时依赖于用于将请求和响应匹配在一起的所谓的相关标识符(使用 WS-Addressing);这与将会话标识符存储在 cookie 中的想法相同,因为 HTTP 是无状态的。
- 有状态的——有一些关于有状态的 WS 的提议,但我对此知之甚少。
Implementation and technology stacks:
实现和技术栈:
- Servlet-- The lowest-level way to implement a WS: you basically parse the request and spit the HTTP response all by yourself.
- EJB-- Since EJB3, EJB can be exposed as web service very easily. Needs an EJB container, of course.
- Apache Axis-- Used to be a popular technology stack which is declining now.
- Apache CXF-- Another popular choice.
- JBossWS-- Yet another popluar choice.
- JAX-WS-- The official web service stack from Sun, very good. So far I know, this replacesJAX-RPC which was simply renamed JAX-WS.
- Servlet—— 实现 WS 的最低级方法:您基本上自己解析请求并吐出 HTTP 响应。
- EJB——从 EJB3 开始,EJB 可以很容易地作为 Web 服务公开。当然,需要一个 EJB 容器。
- Apache Axis——曾经是一种流行的技术栈,现在正在衰落。
- Apache CXF——另一个流行的选择。
- JBossWS——又一个流行的选择。
- JAX-WS-- Sun 的官方 Web 服务堆栈,非常好。到目前为止,我知道,它取代了JAX-RPC,后者只是更名为 JAX-WS。
Related concepts and jargon:
相关概念和术语:
- WSDL-- Defines the contract/interface of the web service, in case of SOAP-based WS.
- Contract-first-- Refers to the fact that that a technology is able to support any WSDL provided upfront. On the contrary to an implementation technology which will generate the WSDL based on the implementation of the web service, in which case the WSDL can not always be customized as necessary
- Profile-- To simplify this mess, they've introduced profiles which are groups of related specifications/capabilities that need to be supported for interoperability. The main one is WS-I Basic Profile.
- UDDI and discovery-- It seems like some people thought the web service would be published in a public register so as to be discoverable by potential consumer. I don't think this vision gained much momentum.
- WSDL——在基于 SOAP 的 WS 的情况下定义 Web 服务的契约/接口。
- 契约优先——指的是一种技术能够支持任何预先提供的 WSDL。与基于 Web 服务的实现生成 WSDL 的实现技术相反,在这种情况下,不能总是根据需要自定义 WSDL
- 配置文件——为了简化这种混乱,他们引入了配置文件,这些配置文件是需要支持互操作性的相关规范/功能组。主要的一个是 WS-I Basic Profile。
- UDDI 和发现——似乎有些人认为 Web 服务会在公共注册中发布,以便潜在消费者可以发现。我不认为这个愿景获得了太多动力。
回答by duffymo
The best explanation I know for "contract first" web services is Spring web service module.
我所知道的对“合同优先”Web 服务的最佳解释是Spring Web 服务模块。

