Jump to content
JasperReports Library 7.0 is now available ×

How to get pdf output from REST API using Spring MVC


dlcathukorala

Recommended Posts

Hi,

I want to get report output as pdf using the REST API. I have written some code to get it using Spring RestTemplate class. But Im getting an error in invoking the service.

Here is the code that i have written

public ResponseEntity runReport(HttpServletRequest request , String reportUri , String parameters , String outFormat) throws IOException{

 
   String serverIp = (String) request.getSession().getAttribute("serverIp");
   String company = (String) request.getSession().getAttribute("company");
   HttpHeaders headers = (HttpHeaders) request.getSession().getAttribute("headers");
 
   headers.setContentType(MediaType.parseMediaType("application/pdf"));
   String filename = "BSJA6.pdf";
   headers.setContentDispositionFormData(filename, filename);
   headers.setCacheControl("no-cache");
   HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
 
   final String uri = "{serverIp}/jasperserver-pro/rest_v2/reports/{reportUri}.{outFormat}?{parameters}";
 
   Map<String, Object> params = new HashMap<String, Object>();
   params.put("serverIp", serverIp);
   params.put("reportUri", reportUri);
   params.put("outFormat", outFormat);
   params.put("parameters", parameters);
 
   RestTemplate restTemplate = new RestTemplate();
 
   ResponseEntity<ResponseEntity> response = restTemplate.exchange(uri, HttpMethod.GET, entity, ResponseEntity.class , params);
 
   return response;  
}

but I'm getting the following error.

WARN  [org.springframework.web.client.RestTemplate] (http-localhost/127.0.0.1:8080-1) GET request for "http://lolcdc1ms:8080/jasperserver-pro/rest_v2/reports/reports/BSJA6.pdf?p_prod%3D%26P_COMP_NAME%3D%26p_branch%3D%26p_date2%3D%26p_date%3D2016-07-19%26P_SCM%3Ddasd%26p_user%3D%26p_rpt_path%3D" resulted in 400 (Bad Request); invoking error handler
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/jasper-restclient].[jasper-restclient]] (http-localhost/127.0.0.1:8080-1) JBWEB000236: Servlet.service() for servlet jasper-restclient threw exception: org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:384) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at com.loits.jasper.rest.repo.ModuleDaoImpl.runReport(ModuleDaoImpl.java:334) [classes:]
at com.loits.jasper.rest.mvc.ModuleController.runReport(ModuleController.java:103) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_17]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_17]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_17]
at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_17]
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at com.loits.jasper.rest.mvc.SessionFilter.doFilter(SessionFilter.java:46) [classes:]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at java.lang.Thread.run(Thread.java:619) [rt.jar:1.6.0_17]
 
any idea regarding this ?
Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Try typing the REST uri directly in a web browser to make sure the uri is correct. If you have multiple organization, you need to have organization id in you uri.

I'm not sure how you're authorization your session either from the code. You need authorization to use REST API.

Link to comment
Share on other sites

@hozawa it works when I try it in the browser. 

And I use authentication service to authenticate and add the response into Httpheader as a cookie. So authentication works fine.

And I was able to get other detais like repository resources (inputControls , dataSources . etc.)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...