下面是我的代码,用于将某些字段从数据库导出到csv输出文件。
Gui可以控制以csv格式导出文件。但是,输出的csv文件为空白(标头除外)。为什么会这样?
Public class somecontroller
{
try
{
List<CompanyContact> companyContactList = contactService.getContacts(companyType);
List<CompanyContactsExportDTO> companyContactsExportDTOS = createCompanyContactsExportDTO(companyContactList);
writeCsvResponse(response,fileName,CompanyContactsExportDTO.class,companyContactsExportDTOS);
log.info("Finished exporting contacts for " + companyType + "company Type");
} catch(Exception e)
{
log.error("Error producing contacts report", e);
//Close CSV writer so it can flush & print headers in case no rows had been printed before.
// csvRecordWriter.close();
//add extra row stating there was an error
response.getWriter().println();
response.getWriter().print(buildCsvErrorMessage());
}
finally
{
response.getWriter().close();
}
private void writeCsvResponse(HttpServletResponse response, String csvFileName, Class annotatedPojoClass, List results) throws Exception
{
response.setContentType("text/csv");
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", csvFileName));
response.setHeader("X-Content-Type-Options", "nosniff");
response.addHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate");
response.addHeader(HttpHeaders.PRAGMA, "no-cache");
response.addHeader(HttpHeaders.EXPIRES, "0");
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(annotatedPojoClass).withHeader();
mapper.writer(schema).writeValue(response.getWriter(), results);
}
}
private List<CompanyContactsExportDTO> createCompanyContactsExportDTO(List<CompanyContact> companyContactList)
{
List<CompanyContactsExportDTO> companyContactsExportDTOS = new ArrayList<>();
for(CompanyContact cc:companyContactList)
{
CompanyContactsExportDTO dto = new CompanyContactsExportDTO();
dto.setCompany(cc.getCompany().getCompanyName());
dto.setContactType(cc.getCategory());
dto.setEmail(cc.getParent().getEmail());
dto.setPhone(cc.getParent().getPhone());
dto.setInfo(cc.getInfo());
dto.setFirstNameLastName(cc.getParent().getFirstName()+ " " +cc.getParent().getLastName());
}
return companyContactsExportDTOS;
}
但导出文件为空。
Jason对象如下所示
@JsonPropertyOrder({
"company",
"contactType",
"info",
"firstNameLastName",
"phone",
"email"
})
public class CompanyContactsExportDTO
{
private String company;
private String contactType;
private String info;
private String firstNameLastName;
private String phone;
private String email;
public CompanyContactsExportDTO()
{
}
public String getCompany()
{
return company;
}
public String getContactType()
{
return contactType;
}
public String getInfo()
{
return info;
}
public String getFirstNameLastName()
{
return firstNameLastName;
}
public String getPhone()
{
return phone;
}
public String getEmail()
{
return email;
}
public void setContactType(String contactType)
{
this.contactType = contactType;
}
public void setPhone(String phone)
{
this.phone = phone;
}
public void setEmail(String email)
{
this.email = email;
}
public void setInfo(String info)
{
this.info = info;
}
public void setCompany(String companyName)
{
this.company = companyName;
}
public void setFirstNameLastName(String firstNameLastName)
{
this.firstNameLastName = firstNameLastName;
}
}
并且从DB派生的对象具有以下结构
public class CompanyContact
{
private Integer id;
private String info;
private boolean isActive;
private CompanyDTO company;//this is embedded object
private String category;
private Contact parent;//this is another embedded object
public CompanyContact()
{
}
//It has other getter and setter methods
}
请让我知道我在做什么错?
谢谢