无法调用,因为" "因为服务为空



我一直在尝试使用一个可以与mysql和jasperreports集成的springboot项目。其想法是,用户可以将任何人添加到数据库中,然后打印包含所有实体的报告。错误出现在我的控制器上的viewReport((函数中。上面说this.userService为空

@Controller
public class AppController {
@Autowired
private UserRepository repo;
private UserService userService;
@GetMapping("")
public String viewHomePage() {
return "index";
}
@GetMapping("/register")
public String showSignUpForm(Model model) {
model.addAttribute("user", new User());

return "signup_form";
}
@GetMapping("/report")
public String generateReport() {
userService.viewReport();

return "index";
}
@PostMapping("/process_register")
public String processRegistration(User user) {
repo.save(user);

return "register_success";
}
}

这是我的控制器。错误为userService.viewReport((;

@Service
public class UserService {

@Autowired
private UserRepository repo;

public String viewReport() {
Log.info("Preparing the pdf report via Jasper.");
try {
createPdfReport(repo.findAll());
Log.info("File successfully saved at given path.");
} catch (final Exception e) {
Log.error("Some error has occured while preparing PDF document.");
e.printStackTrace();
}
return "Done in ViewReport";
}

public void createPdfReport(final List<User> users) throws JRException {
//Fetching .jrxml file from resources folder.
final InputStream stream = this.getClass().getResourceAsStream("/report.jrxml");

//compile report from .jrxml to .jasper
final JasperReport report = JasperCompileManager.compileReport(stream);

//Fetch users from the data source
final JRBeanCollectionDataSource source = new JRBeanCollectionDataSource(users);

//Adding the additional parameters to the pdf
final Map<String, Object> parameters = new HashMap<>();
parameters.put("CreatedBy", "edu.sru.Smith" );

//filling report with data from database
final JasperPrint print = JasperFillManager.fillReport(report, parameters, source);

//place file in c:drive
final String filePath = "C://CPSC-488";
//export report to pdf file
JasperExportManager.exportReportToPdfFile(print, filePath + "Employee_Report.pdf");

}

} ```
That is my service class that autofills with the data from the MySQL database. I have tried adding a number of annotations and other things, but I haven't had success with anything. I'm honestly just new to this whole springboot thing and annotations overall and I have no idea where to go from here.

@Autowired注释应该放在每个需要注入的字段上。

@Autowired
private UserRepository repo;
@Autowired
private UserService userService;

private UserService userService;之前缺少@Autowired注释。

应该是

@Autowired
private UserRepository repo;
@Autowired
private UserService userService;

相关内容

最新更新