我编写了一个粗略的程序,该程序的目标是成为一个能够接收请求的程序。请求的语言是 SQL cmd,它将请求发送到 PostgreSQL 数据库,并将响应作为来自 PostgreSQL 数据库的数据发送,然后获取数据。在jrxml文件中插入或填充,然后最后转换为base64 PDF, 我试图编写它,但自从单词开始生成以来发生了错误。
使用的程序:eclipse、spring boot 3及以上以及Java 17、21、PostgreSQL数据库。
错误
[2m2024-01-18T15:35:33.425+07:00[0;39m [32m INFO[0;39m [35m32224[0;39m [2m---[0;39m [2m[nio-8080-exec-2][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Completed initialization in 2 ms
start generate
[2m2024-01-18T15:35:34.012+07:00[0;39m [31mERROR[0;39m [35m32224[0;39m [2m---[0;39m [2m[nio-8080-exec-2][0;39m [2m[0;39m[36mo.a.c.c.C.[.[.[/].[dispatcherServlet] [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: net.sf.jasperreports.engine.JRException: Class not found when loading object from file: C:\Users\Teamking\eclipse-workspace\Npdf\target\classes\pdf.jasper.] with root cause
java.lang.ClassNotFoundException: net.sf.jasperreports.compilers.ReportExpressionEvaluationData
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525) ~[na:na]
at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
at java.base/java.lang.Class.forName(Class.java:467) ~[na:na]
at java.base/java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:773) ~[na:na]
at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.resolveClass(ContextClassLoaderObjectInputStream.java:89) ~[jasperreports-6.12.2.jar:6.12.2-75c5e90a222ab406e416cbf590a5397028a52de3]
at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:2034) ~[na:na]
at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1898) ~[na:na]
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2224) ~[na:na]
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1733) ~[na:na]
at java.base/java.io.ObjectInputStream$FieldValues.<init>(ObjectInputStream.java:2606) ~[na:na]
at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2457) ~[na:na]
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2257) ~[na:na]
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1733) ~[na:na]
at java.base/java.io.ObjectInputStream$FieldValues.<init>(ObjectInputStream.java:2606) ~[na:na]
at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2457) ~[na:na]
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2257) ~[na:na]
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1733) ~[na:na]
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:509) ~[na:na]
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:467) ~[na:na]
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:140) ~[jasperreports-6.12.2.jar:6.12.2-75c5e90a222ab406e416cbf590a5397028a52de3]
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:116) ~[jasperreports-6.12.2.jar:6.12.2-75c5e90a222ab406e416cbf590a5397028a52de3]
at net.sf.jasperreports.engine.JasperFillManager.getReportSource(JasperFillManager.java:1105) ~[jasperreports-6.12.2.jar:6.12.2-75c5e90a222ab406e416cbf590a5397028a52de3]
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:652) ~[jasperreports-6.12.2.jar:6.12.2-75c5e90a222ab406e416cbf590a5397028a52de3]
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:1020) ~[jasperreports-6.12.2.jar:6.12.2-75c5e90a222ab406e416cbf590a5397028a52de3]
at com.example.demo.service.ReportGenerateService.generate(ReportGenerateService.java:30) ~[classes/:na]
at com.example.demo.controller.ExportPDFpart1Controller.exportReportFromJrxml(ExportPDFpart1Controller.java:41) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:262) ~[spring-web-6.1.2.jar:6.1.2]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:190) ~[spring-web-6.1.2.jar:6.1.2]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) ~[spring-webmvc-6.1.2.jar:6.1.2]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:917) ~[spring-webmvc-6.1.2.jar:6.1.2]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:829) ~[spring-webmvc-6.1.2.jar:6.1.2]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.1.2.jar:6.1.2]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089) ~[spring-webmvc-6.1.2.jar:6.1.2]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) ~[spring-webmvc-6.1.2.jar:6.1.2]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) ~[spring-webmvc-6.1.2.jar:6.1.2]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914) ~[spring-webmvc-6.1.2.jar:6.1.2]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:590) ~[tomcat-embed-core-10.1.17.jar:6.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) ~[spring-webmvc-6.1.2.jar:6.1.2]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) ~[tomcat-embed-core-10.1.17.jar:6.0]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) ~[tomcat-embed-websocket-10.1.17.jar:10.1.17]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-6.1.2.jar:6.1.2]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.1.2.jar:6.1.2]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-6.1.2.jar:6.1.2]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.1.2.jar:6.1.2]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-6.1.2.jar:6.1.2]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.1.2.jar:6.1.2]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.17.jar:10.1.17]
at java.base/java.lang.Thread.run(Thread.java:840) ~[na:na]
这是我的代码:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Npdf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Npdf</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.12.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
导出JR请求
package com.example.demo.bean;
public class ExportJRRequest {
private String sqlCommand;
public String getSqlCommand() {
return sqlCommand;
}
public void setSqlCommand(String sqlCommand) {
this.sqlCommand = sqlCommand;
}
}
导出JRResponse
package com.example.demo.bean;
public class ExportJRResponse {
private String pdfBase64;
public String getPdfBase64() {
return pdfBase64;
}
public void setPdfBase64(String pdfBase64) {
this.pdfBase64 = pdfBase64;
}
}
导出PDFpart1控制器
package com.example.demo.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.bean.ExportJRRequest;
import com.example.demo.bean.ExportJRResponse;
import com.example.demo.model.EmployeeModel;
import com.example.demo.service.InquiryService;
import com.example.demo.service.ReportGenerateService;
import net.sf.jasperreports.repo.ReportCompiler;
@RestController
@RequestMapping("export")
public class ExportPDFpart1Controller {
@Autowired
private InquiryService inquiryService;
@Autowired
private ReportGenerateService reportGenerateService;
@PostMapping(path ="/part1")
public @ResponseBody ExportJRResponse exportReportFromJrxml( @RequestBody ExportJRRequest req) throws Exception {
ExportJRResponse res = new ExportJRResponse();
//1
List<EmployeeModel> data = inquiryService.executeEmployeeQuery(req.getSqlCommand());
//2
String base64 = reportGenerateService.generate(data);
res.setPdfBase64(base64);
return res;
}
}
员工模型
package com.example.demo.model;
import java.io.Serializable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "employee")
public class EmployeeModel implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long empid;
@Column(name = "name")
private String name;
@Column(name = "salary")
private String salary;
@Column(name = "dept")
private String dept;
@Column(name = "position")
private String position;
public EmployeeModel() {
super();
}
public EmployeeModel(long empid, String name, String salary, String dept, String position) {
super();
this.empid = empid;
this.name = name;
this.salary = salary;
this.dept = dept;
this.position = position;
}
public long getEmpid() {
return empid;
}
public void setEmpid(long empid) {
this.empid = empid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
员工存储库
package com.example.demo.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.model.EmployeeModel;
public interface EmployeeRepository extends JpaRepository<EmployeeModel, Long> {
List<EmployeeModel> findAll();
}
咨询服务
package com.example.demo.service;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.model.EmployeeModel;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
@Service
public class InquiryService {
@Autowired
private EntityManager entityManager;
public List<EmployeeModel> executeEmployeeQuery(String sql) {
Query query = entityManager.createNativeQuery(sql, EmployeeModel.class);
return query.getResultList();
}
}
报告生成服务
package com.example.demo.service;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import com.example.demo.model.EmployeeModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
@Service
public class ReportGenerateService {
public String generate(List<EmployeeModel> data ) throws FileNotFoundException, JRException {
System.out.println("start generate");
String sourceFileName = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "pdf.jasper").getAbsolutePath();
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(data);
Map parameters = new HashMap();
JasperPrint jasperPrint = JasperFillManager.fillReport(sourceFileName, parameters, beanColDataSource);
byte[] bPdf = JasperExportManager.exportReportToPdf(jasperPrint);
return Base64.getEncoder().encodeToString(bPdf);
}
}
请给我一些建议,因为这是我第一次开始这份工作,这是我需要完成的项目计划。目前,我需要完成第1部分的API,如图所示:
堆栈跟踪表明您使用jasperreports-6.12.2.jar来运行报告,但编译后的报告(*.jasper文件)引用了JasperReports 6.13.0中引入的net.sf.jasperreports.compilers.ReportExpressionEvaluationData。
您需要确保用于运行报告的 JasperReports 版本与用于生成编译报告的版本相同或更新。