I had the same problem, but at the top, use the RESTFUL webservice for this and I have a complex data object that I have to publish.
My solution: as a jQuery plugin, I create a temp form and submit it. But I am sending a data object as a parameter with json content (I am using AngularJS here, but it should also work with jQuery.param() .)
JavaScript:
$('<form target="_blank" action="' + appConstants.restbaseurl + '/print/pdf" method="POST">' + "<input name='data' value='" + angular.toJson($scope.versicherung) + "' />" + '</form>').appendTo('body').submit().remove();
on the server side we use the CXF REST Service with the JACKSON Provider:
Spring Configuration:
<jaxrs:server id="masterdataService" address="/"> <jaxrs:serviceBeans> <ref bean="printRestServiceBean" /> </jaxrs:serviceBeans> <jaxrs:providers> <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" /> <bean class="de.controller.ExceptionHandler" /> </jaxrs:providers> </jaxrs:server>
in the controller, I extracted the parameter and converted it back to Java Pojo:
package de.controller; import javax.ws.rs.Consumes; import javax.ws.rs.FormParam; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; @Path(Constants.PRINT_PATH) @Consumes({ MediaType.APPLICATION_JSON, "application/x-www-form-urlencoded"}) @Produces("application/pdf; charset=UTF-8") public class PrintRestController { @Autowired private PrintService printService; @POST @Produces("application/pdf") @Path("/pdf") public Response getPDF(@FormParam("data") String data) { return printService.getPDF(json2Versicherung(data)); } private Versicherung json2Versicherung(String data) { Versicherung lVersicherung = null; try { ObjectMapper mapper = new ObjectMapper(); lVersicherung = mapper.readValue(data, Versicherung.class); } catch(Exception e) { LOGGER.error("PrintRestController.json2Versicherung() error", e); } return lVersicherung; } }
in PrintService I create a pdf file and answer:
@Override public Response getPDF(Versicherung pVersicherung) { byte[] result = ...
This solution works for all browsers (even for IE9 that cannot handle data URLs), and on tablets and smartphones, and it has no problems with popupblockers
Anti-g Sep 17 '14 at 12:22 2014-09-17 12:22
source share