This is not supported by JAXB because you do not want to serialize binary data into XML, but you can usually do this at a higher level using JAXB. The way I do this is that the web services (SOAP and REST) use MIME multipart / mixed messages (check the multipart specification ). Originally designed for emails, it works great for sending xml with binary data, and most web services, such as an axis or jersey, support it in an almost transparent way.
Here is an example of sending an object in XML along with a binary file with a REST web service using Jersey with the jersey-multipart extension .
XML object
@XmlRootElement public class Book { private String title; private String author; private int year;
Client
byte[] bin = some binary data... Book b = new Book(); b.setAuthor("John"); b.setTitle("wild stuff"); b.setYear(2012); MultiPart multiPart = new MultiPart(); multiPart.bodyPart(new BodyPart(b, MediaType.APPLICATION_XML_TYPE)); multiPart.bodyPart(new BodyPart(bin, MediaType.APPLICATION_OCTET_STREAM_TYPE)); response = service.path("rest").path("multipart"). type(MultiPartMediaTypes.MULTIPART_MIXED). post(ClientResponse.class, multiPart);
Server
@POST @Consumes(MultiPartMediaTypes.MULTIPART_MIXED) public Response post(MultiPart multiPart) { for(BodyPart part : multiPart.getBodyParts()) { System.out.println(part.getMediaType()); } return Response.status(Response.Status.ACCEPTED). entity("Attachements processed successfully."). type(MediaType.TEXT_PLAIN).build(); }
I tried to send a file with 110917 bytes. Using wirehark, you can see that the data is transmitted directly through HTTP as follows:
Hypertext Transfer Protocol POST /org.etics.test.rest.server/rest/multipart HTTP/1.1\r\n Content-Type: multipart/mixed; boundary=Boundary_1_353042220_1343207087422\r\n MIME-Version: 1.0\r\n User-Agent: Java/1.7.0_04\r\n Host: localhost:8080\r\n Accept: text/html, image/gif, image/jpeg\r\n Connection: keep-alive\r\n Content-Length: 111243\r\n \r\n [Full request URI: http://localhost:8080/org.etics.test.rest.server/rest/multipart] MIME Multipart Media Encapsulation, Type: multipart/mixed, Boundary: "Boundary_1_353042220_1343207087422" [Type: multipart/mixed] First boundary: --Boundary_1_353042220_1343207087422\r\n Encapsulated multipart part: (application/xml) Content-Type: application/xml\r\n\r\n eXtensible Markup Language <?xml <book> <author> John </author> <title> wild stuff </title> <year> 2012 </year> </book> Boundary: \r\n--Boundary_1_353042220_1343207087422\r\n Encapsulated multipart part: (application/octet-stream) Content-Type: application/octet-stream\r\n\r\n Media Type Media Type: application/octet-stream (110917 bytes) Last boundary: \r\n--Boundary_1_353042220_1343207087422--\r\n
As you can see, binary data is sent with an octet stream without space loss, which contradicts what happens when sending inline binary data to xml. This is just a very low MIME envelope. With SOAP the principle is the same (just that it will have a SOAP envelope).
Duarte meneses
source share