Java MTOM 是如何工作的 + 示例代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2619435/
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 does MTOM work + sample code
提问by zengr
I am trying to make a very simple web-service which does the following:
我正在尝试制作一个非常简单的网络服务,它执行以下操作:
- The client hits the web service requesting a file.
- The web service's service class queries a hashtable which has the key (search query) and the value as the base64encoded value of a file (say a pdf)
- Now,I need to use MTOM to return the base64encoded value stored in the hashtable to the client.
- 客户端访问请求文件的 Web 服务。
- Web 服务的服务类查询一个哈希表,该哈希表具有键(搜索查询)和作为文件(例如 pdf)的 base64encoded 值的值
- 现在,我需要使用 MTOM 将存储在哈希表中的 base64 编码值返回给客户端。
It's upto the client to decode it and convert it to pdf.
由客户端对其进行解码并将其转换为 pdf。
So, here are my questions:
所以,这里是我的问题:
I understand we encode files to base64 for transmission via web service, but where and how does MTOM come into the picture there?
Can some one provide me a simple method which uses MTOM and sends the data back. Do we need to specify something in the WSDL too? or a simple String return type would suffice? Why/Why not?
我知道我们将文件编码为 base64 以通过 Web 服务进行传输,但是 MTOM 在哪里以及如何出现在那里?
有人可以为我提供一种使用 MTOM 并将数据发回的简单方法。我们是否也需要在 WSDL 中指定一些内容?或者一个简单的 String 返回类型就足够了?为什么/为什么不?
Thanks
谢谢
I have seen this code. It uses a lot of annotations, I just need a simple java code using MTOM. New to J2EE HERE :)
我看过这段代码。它使用了很多注释,我只需要一个简单的使用 MTOM 的 java 代码。J2EE 新手在这里:)
UPDATE 1
更新 1
Decent enough explanation here, now I need to understand it from the implementation level. Good info here, but for .Net
这里的解释已经足够了,现在我需要从实现层面来理解它。这里的信息很好,但对于 .Net
UPDATE 2
更新 2
What should be the return type of the method "createMTOM(int id)"??
方法“createMTOM(int id)”的返回类型应该是什么??
采纳答案by bmargulies
You seem to be mixing up implementation and protocol.
您似乎混淆了实现和协议。
The specification for MTOM is here. Thispage has sample messages.
If you are using a JAX-WS environment, you most assuredly use Annotations
to turn it on. If you are rolling soap messages by hand, you write whatever Java code you need to write to produce it. Essentially, MTOM means that the content of the data move to an attachment, which is a separate MIME part of the message, and is referenced through an xop:Include element with an href to the MIME part.
如果您使用的是 JAX-WS 环境,那么您肯定会使用Annotations
将其打开。如果您手动滚动soap 消息,您需要编写生成它所需的任何Java 代码。本质上,MTOM 意味着数据的内容移动到附件,它是消息的一个单独的 MIME 部分,并通过 xop:Include 元素和指向 MIME 部分的 href 进行引用。
The schema has to say base64Binary as the element type, but additional attributes give hints as to the interpretation of the bytes: xmime:contentType and xmime:expectedContentType. In my experience, the JAX-B reference implementation has some limitations on the use of these. You can't, for example, get it to automatically convert text back and forth to bytes. In a typical kit like CXF or Glassfish, you put data into a DataHandler and the kit turns it into the attachment.
架构必须将 base64Binary 指定为元素类型,但附加属性会提供有关字节解释的提示:xmime:contentType 和 xmime:expectedContentType。根据我的经验,JAX-B 参考实现对这些的使用有一些限制。例如,您不能让它自动将文本来回转换为字节。在 CXF 或 Glassfish 等典型工具包中,您将数据放入 DataHandler 中,工具包将其转换为附件。
回答by intltechventures
A good detailed example / discussion is provided in the IBM Redbook: Developing Web Services Applications, see pages 64-74
IBM 红皮书:开发 Web 服务应用程序中提供了一个很好的详细示例/讨论,请参阅第 64-74 页