Open PDF returned by REST Web Service

2019-09-03 17:34发布

问题:

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) {
...
}

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.

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?

回答1:

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;
}

And to open the PDF document use postman chrome extension



标签: java json rest pdf