In this tutorial, I show you how to generate a PDF with Jasperreport and Spring Boot .
application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/YOURDATABASE?autoReconnect=true spring.datasource.username=YOURUSERNAME spring.datasource.password=YOURPASSWORD spring.jpa.generate-ddl=true
net.sf.jasperreports jasperreports 6.16.0 org.springframework spring-context-support
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServiceJpasql001Application {
public static void main(String[] args) {
SpringApplication.run(ServiceJpasql001Application.class, args);
}
}
package com.example.demo.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student{
@Id
@Column(name="stid")
private int stid;
@Column(name="name")
private String name;
@Column(name="programme")
private String Programme;
public int getStid() {
return stid;
}
public void setStid(int stid) {
this.stid = stid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProgramme() {
return Programme;
}
public void setProgramme(String programme) {
Programme = programme;
}
public Student(int stid, String name, String programme) {
super();
this.stid = stid;
this.name = name;
Programme = programme;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
package com.example.demo.repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import com.example.demo.model.Student; public interface StudentRepository extends JpaRepository{ List findByName(String name); }
package com.example.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public Student addStudent(Student student) {
return studentRepository.save(student);
}
public void deleteStudent(Student student) {
studentRepository.delete(student);
}
public List getStudent() {
return studentRepository.findAll();
}
public List getStudentByName(String name) {
return studentRepository.findByName(name);
}
}
package com.example.demo.controller;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.Student;
import com.example.demo.service.StudentService;
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.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping("/all")
public List getStudent() {
return studentService.getStudent();
}
@RequestMapping("/studentByName/{name}")
public List getStudentByName(@PathVariable String name) {
return studentService.getStudentByName(name);
}
@RequestMapping("/save")
public Student saveStudent(@RequestBody Student student) {
return studentService.addStudent(student);
}
@RequestMapping("/delete")
public void deleteStudent(@RequestBody Student student) {
studentService.deleteStudent(student);
}
@RequestMapping("/pdf")
public void getReportsinPDF(HttpServletResponse response) throws JRException, IOException {
//Compiled report
InputStream jasperStream = (InputStream) this.getClass().getResourceAsStream("/StudentList.jasper");
//Adding attribute names
Map params = new HashMap<>();
params.put("stid","stid");
params.put("name","name");
params.put("programme","programme");
// Fetching the student from the data database.
final JRBeanCollectionDataSource source = new JRBeanCollectionDataSource(studentService.getStudent());
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, source);
response.setContentType("application/x-pdf");
response.setHeader("Content-disposition", "inline; filename=StudentList.pdf");
final ServletOutputStream outStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
}
}
No comments:
Post a Comment