Java SOAP Web 服务是否仅支持“POST”http 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26339317/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:16:50  来源:igfitidea点击:

Do SOAP Web services support only "POST" http method

javaweb-serviceshttppostsoap

提问by evgeniy44

I faced this question on one of interviews, so could you please tell whether SOAP Web services support only "POST" http method or there is some way to accept other methods on the server side?

我在一次采访中遇到了这个问题,所以你能告诉我 SOAP Web 服务是只支持“POST”http 方法还是有某种方法可以接受服务器端的其他方法?

采纳答案by Eaque

I always used POST but according to the W3C standard, SOAP supports both POST and GET methods.

我一直使用 POST 但根据W3C 标准,SOAP 支持 POST 和 GET 方法。

Edit: After some research, it seems that it's not completely true, as you can see here. It is theoreticallypossible to use GET because POST and GET are methods of HTTP transport protocol and SOAP can be used over HTTP.

编辑:经过一些研究,似乎并不完全正确,正如您在此处看到的那样。这是理论上可能使用GET因为POST和GET是可用于通过HTTP传输协议和SOAP的HTTP方法。

But as you know, GET includes the request in the query string. SOAP requests (XML messages) are usually too complex and verbose to be included in the query string, so almost every implementation (for example JAX-WS) supports only POST.

但如您所知,GET 将请求包含在查询字符串中。SOAP 请求(XML 消息)通常过于复杂和冗长,无法包含在查询字符串中,因此几乎每个实现(例如 JAX-WS)都只支持 POST。

回答by Vahagn Nahapetyan

Thread is three years old but I think that there will be still a lot of people who will give this same question to themselves and will find wrong answer in the web. The answer to the question is no, GET method can be used too.

Thread 已经三年了,但我认为仍然会有很多人给自己提出同样的问题,并会在网络上找到错误的答案。问题的答案是否定的,也可以使用 GET 方法。

According to SOAP specification, which can found here: https://www.w3.org/TR/2007/REC-soap12-part0-20070427/#transportboth GET and POST methods can be used to exchange SOAP messages over http. The use of the HTTP POST method for conveying SOAP messages in the bodies of HTTP request uses a pattern called SOAP request-response message exchange pattern. In the case of HTTP GET a pattern is used called SOAP response message exchange pattern. The main difference of this two patterns is:

根据 SOAP 规范,可在此处找到:https: //www.w3.org/TR/2007/REC-soap12-part0-20070427/#transportGET 和 POST 方法均可用于通过 http 交换 SOAP 消息。使用 HTTP POST 方法在 HTTP 请求的主体中传送 SOAP 消息使用称为SOAP 请求-响应消息交换模式 的模式。在 HTTP GET 的情况下,使用称为SOAP 响应消息交换模式的模式。这两种模式的主要区别在于:

The first type of interaction allows for the use of data within the body of a HTTP POST to create or modify the state of a resource identified by the URI to which the HTTP request is destined. The second type of interaction pattern offers the ability to use a HTTP GET request to obtain a representation of a resource without altering its state in any way. In the first case, the SOAP-specific aspect of concern is that the body of the HTTP POST request is a SOAP message which has to be processed (per the SOAP processing model) as a part of the application-specific processing required to conform to the POST semantics. In the second case, the typical usage that is forseen is the case where the representation of the resource that is being requested is returned not as a HTML, or indeed a generic XML document, but as a SOAP message. That is, the HTTP content type header of the response message identifies it as being of media type "application/soap+xml"

第一种类型的交互允许使用 HTTP POST 正文中的数据来创建或修改由 HTTP 请求所指向的 URI 标识的资源的状态。第二种交互模式提供了使用 HTTP GET 请求来获取资源表示而不以任何方式改变其状态的能力。在第一种情况下,与 SOAP 相关的特定方面是 HTTP POST 请求的主体是一个 SOAP 消息,它必须被处理(根据 SOAP 处理模型)作为应用程序特定处理的一部分,以符合POST 语义。在第二种情况下,可以预见的典型用法是所请求的资源的表示不是作为 HTML 返回的,或者实际上不是一般的 XML 文档,而是作为 SOAP 消息返回。那是,

So both GET and POST methods can be used. The other thing is that in practice mostly POST method is used.

所以 GET 和 POST 方法都可以使用。另一件事是在实践中主要使用 POST 方法。

The bad thing is that when comparing RESTful services with SOAP services, as an advantage of REST people are bringing caching, which is not available in SOAP, because SOAP uses only POST. This is totally wrong.

不好的是,当比较 RESTful 服务和 SOAP 服务时,作为 REST 的一个优势,人们带来了缓存,这在 SOAP 中是不可用的,因为 SOAP 只使用 POST。这是完全错误的。

回答by SantanuB

This is an implementation of GET in SOAP:

这是 SOAP 中 GET 的实现:

@WebServiceProvider(targetNamespace="http://attachment.service.soap.com/download")
@ServiceMode(value = javax.xml.ws.Service.Mode.MESSAGE)
@BindingType(value = HTTPBinding.HTTP_BINDING)
public final class ImageDownloadServiceProvider implements Provider<DataSource> {
    @Resource
    private WebServiceContext wsContext;

    @Override
    public DataSource invoke(DataSource request) {
        if (wsContext == null)
            throw new RuntimeException("dependency injection failed on wsContext");
        MessageContext msgContext = wsContext.getMessageContext();
        HttpExchange exchange = (HttpExchange) msgContext.get("com.sun.xml.internal.ws.http.exchange");
        String filename = exchange.getRequestURI().getQuery().replace("file=", "");
        switch ((String) msgContext.get(MessageContext.HTTP_REQUEST_METHOD)) {
        case "GET":
            return doGet(filename);
        default:
            throw new HTTPException(405);
        }
    }

    private DataSource doGet(String filename) {
        FileDataSource fds = new FileDataSource(filename);
        MimetypesFileTypeMap mtftm = new MimetypesFileTypeMap();
        mtftm.addMimeTypes("image/jpeg jpg");
        fds.setFileTypeMap(mtftm);
        return fds;
    }