I really doubt that such an exception may be fixed by upgrading
jasperreport (NotSerializableException is pure wicket exception, not a
jasper one).
The stacktrace shows you that the problem is here:
final java.io.InputStream
com.homeaccount.web.jasper.JRResource$1.val$report
[class=java.io.ByteArrayInputStream] <----- field that is not serializable
so nothing to do with jasperreport. Just declare your streams as
"transient"
and lazy-init it in an initialisation method. And don't forget to close
them as well...
I you want to, here is how I've my generation of pdf:
# Part that generate a PDF to a byte array
public byte[] create(int orderid) {
final JasperReport report;
Order o = getOrder(orderid);
try {
report = (JasperReport) JRLoader.loadObject(InvoiceCreator.class
.getResourceAsStream(
"/jasperreports/Invoice/Invoice.jasper"));
} catch (JRException e) {
throw new RuntimeException(e);
}
final Map params = new HashMap();
params.put("basedir", "/jasperreports/Invoice/");
params.put("curr", o.getCurrency());
params.put("orderid", o.getId());
params.put("shopid", o.getShops().getId());
params.put("REPORT_LOCALE", new Locale("fr");
byte[] bytes;
DataSource dataSource = Globals.getDataSource();
Connection connection = DataSourceUtils.getConnection(dataSource);
try {
bytes = JasperRunManager.runReportToPdf(report, params,
connection);
} catch (Exception e) {
bytes = null;
throw new RuntimeException(e);
} finally {
DataSourceUtils.releaseConnection(connection, dataSource);
}
return bytes;
}
# Part that write that data to the wicket's stream
private static Link newInvoiceLink(String string, final Integer id) {
return new Link(string) {
@Override
public void onClick() {
getRequestCycle().setRequestTarget(new
ResourceStreamRequestTarget(new AbstractResourceStream() {
private static final long serialVersionUID = 1L;
private transient InputStream is = null;
public void close() throws IOException {
if (is != null) {
is.close();
}
is = null;
}
public InputStream getInputStream() {
if (is == null) {
is = new ByteArrayInputStream(getInvoice(id));
}
return is;
}
}, "Invoice #" + id + ".pdf"));
}
};
}
This way you won't have any serializableexception anymore.
Hope this help
noon wrote:
I had some similar issues and I solved them by upgrading the JasperReports
from 2.x to 3.0.0.
novotny wrote:
Hi,
Ok I found in wicketstuff, a JRPdfResource class that does the jasper to
pdf conversion for me
and I added code to do this:
InputStream is = getClass().getResourceAsStream("/test.jasper");
JRResource pdfResource = new JRPdfResource(is);
pdfResource.setReportParameters(new HashMap<String, Object>());
add(new ResourceLink("print", pdfResource));
But when I run it I get
org.apache.wicket.util.io.SerializableChecker$WicketNotSerializableException:
Unable to serialize class: java.io.ByteArrayInputStream
Field hierarchy is:
2 [class=com.homeaccount.web.loanoptions.MortgageResultsPage, path=2]
private java.lang.Object org.apache.wicket.MarkupContainer.children
[class=[Ljava.lang.Object;]
private java.lang.Object
org.apache.wicket.MarkupContainer.children[8]
[class=org.apache.wicket.markup.html.link.ResourceLink, path=2:print]
private final org.apache.wicket.Resource
org.apache.wicket.markup.html.link.ResourceLink.resource
[class=com.homeaccount.web.jasper.JRPdfResource]
private
com.homeaccount.web.jasper.JRResource$JasperReportFactory
com.homeaccount.web.jasper.JRResource.jasperReportFactory
[class=com.homeaccount.web.jasper.JRResource$1]
final java.io.InputStream
com.homeaccount.web.jasper.JRResource$1.val$report
[class=java.io.ByteArrayInputStream] <----- field that is not serializable
at
org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:349)
at
org.apache.wicket.util.io.SerializableChecker.checkFields(SerializableChecker.java:618)
at
org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:541)
I checked and JRResources implements Serializable, is there a way I can
get Wicket to avoid serializing this?
I was also thinking maybe I could do something like this:
add(new Link("print") {
public void onClick() {
InputStream is =
getClass().getResourceAsStream("/test.jasper");
JRResource pdfStream = new JRPdfResource(is);
pdfStream.setReportParameters(new HashMap<String,
Object>());
getRequestCycle().setRequestTarget(new
ResourceStreamRequestTarget(pdfStream).setFileName("test.pdf"));
}
});
But turns out JRResource extends DynamicWebResource and does not implement
IResourceStream so not sure if this can be made to work...
Thanks, Jason
Pills wrote:
Hi,
look at RequestCycle#setRequestTarget and ResourceStreamRequestTarget
novotny wrote:
Hi,
I'm learning Jasper to create/display a PDF when a link is clicked and
the
template code I have looks like:
ServletOutputStream servletOutputStream = response.getOutputStream();
InputStream reportStream =
getServletConfig().getServletContext().getResourceAsStream("/reports/FirstReport.jasper");
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), new
JREmptyDataSource());
response.setContentType("application/pdf");
servletOutputStream.flush();
servletOutputStream.close();
How should I acccess the servlet objects, or is there a better way to do
this that is the "wicket way(tm)"?
Much appreciated, Jason
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org