REST Web服务返回的打开PDF [英] Open PDF returned by REST Web Service

查看:101
本文介绍了REST Web服务返回的打开PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JAXB REST Web服务接收请求JSON的POST,并且应该返回一个PDF文档作为响应:

I have a JAXB REST Web Service getting in request a POST of a JSON and should return a PDF document in response:

@POST
@Path("getReceipt")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ "application/pdf" })
public Response getReceipt(InputStream incomingData, @Context HttpServletRequest httpRequest) {
...
}

我正在尝试使用Advanced Rest Client(Google Chrome插件)对其进行测试,该响应显示为以下内容:

I'm trying to test it with Advanced Rest Client (Google Chrome plugin), which is showing the following as response:

%PDF-1.4
%����
3 0 obj
<</C[0 0 1]/Border[0 0 0]/A<</URI(https://www.totalerg.it/)/S/URI>>/Subtype/Link/Rect[491.43 723.5 557 733.5]>>
endobj
4 0 obj
<</C[0 0 1]/Border[0 0 0]/A<</URI(https://www.totalerg.it/)/S/URI>>/Subtype/Link/Rect[256.65 274.5 322.22 284.5]>>
endobj
5 0 obj
<</Length 993/Filter/FlateDecode>>stream
x��U�n�8}�W�[��Ð����lG�j�릲�}Y0kpaK�,%���s�3vHY����"9�̙G/%>�9��gD������ҙ;�(  \|:+WP�h�|D�zo�����)������h�������L2CR��a�%L�
��׷�:��&��g L���1ϲ�Oq���9M�[<�-�ǹ����r���
wL�2�:W�m#��J��׹\*��":��Ld7�]
�3�6�E��Kn_<�}�;���,��g�����uk��D^O��6~��Y����]�=1ٸ�gq:���v�l��"��o��x��ǔ�a�09�3H���VeSY?��,�A����l��^*(��W�BE#J�PKX��Fк�s�^��
�H�)��� ��V��
�k<���FntUj<��!F�/
���U#dU#�q�]ZzUnd�I��Z�w�w'��%��l�4�^!G���~��Ƅ�n��̯�?Ԫ,&�U�1teT"u^���r�P�Bw�:YkLX_:�������ī��Mw��P"���\!%���7����zWO}Yp���h�8�����n�]�� ;x�o�2w�38�ś.��/�~�+e"pC����ڳk�]�c#
�����E�{�}�nZ��8�35T�IC+��6�6����E���">J��AZ���F��#��j*����*7�#Pm]����e�e���U�,�؍�!�j�Z��B�de�ҿ�������A֍i����`\T0�0r������Ȇ �G#A��������϶��f���'��׋�͵�,6c�.�4�O�Y:6C.6�}�!]éy�{E߱?��>�<Ƭ�u�y$��9��L���f�]�T��ܸ�/w���������^qj΢[�r��U)g1b������W���Ǥ�*1�؆�������Ȁ8�F�c�ٯB^�bk�n=�?u��x�;�]�H0rsg���tI[x�����G�0�����@p�EW��B�������L�
endstream
endobj
7 0 obj
<</Parent 6 0 R/Contents 5 0 R/Type/Page/Resources<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]/Font<</F1 1 0 R/F2 2 0 R>>>>/MediaBox[0 0 595 842]/Annots[3 0 R 4 0 R]>>
endobj
1 0 obj
<</BaseFont/Helvetica/Type/Font/Encoding/WinAnsiEncoding/Subtype/Type1>>
endobj
2 0 obj
<</BaseFont/Helvetica-Bold/Type/Font/Encoding/WinAnsiEncoding/Subtype/Type1>>
endobj
6 0 obj
<</ITXT(2.1.7)/Type/Pages/Count 1/Kids[7 0 R]>>
endobj
8 0 obj
<</Type/Catalog/Pages 6 0 R>>
endobj
9 0 obj
<</Producer(iText 2.1.7 by 1T3XT)/ModDate(D:20150907102806+02'00')/CreationDate(D:20150907102806+02'00')>>
endobj
xref
0 10
0000000000 65535 f 
0000001518 00000 n 
0000001606 00000 n 
0000000015 00000 n 
0000000142 00000 n 
0000000272 00000 n 
0000001699 00000 n 
0000001332 00000 n 
0000001762 00000 n 
0000001807 00000 n 
trailer
<</Root 8 0 R/ID [<f16ea10e09183af8aff079f97cfac53f><fe60918ac5b80276ed8a082dedebf230>]/Info 9 0 R/Size 10>>
startxref
1929
%%EOF

这种二进制文件的文本版本与我看到的那种文本文件有所不同(更多的带问号的字符代替其他字符).

This text version of binary file is different (more question marked characters in place of other ones) from the one I can see opening the binary file from file system with a text editor.

我的问题是:我如何测试发布JSON请求并能够在响应中打开PDF文档的服务?我应该使用其他工具还是构建表单?

My question is: how could I test this service posting the JSON request and being able to open PDF document in the response? Should I use another tool, or build a form?

推荐答案

您只需要使用适当的媒体类型返回pdf字节数组,其余的工作将由浏览器完成.即:

You just need to return the pdf byte array using the proper media type and the browser will do the rest. ie:

@POST
@Path("getReceipt")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ "application/pdf" })
public byte[] getReceipt(InputStream incomingData) {
     return your_pdf_byte_array;
}

要打开PDF文档,请使用邮递员chrome扩展名

And to open the PDF document use postman chrome extension

这篇关于REST Web服务返回的打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆