我做了一些研究,但找不到解决这个问题的方法。当用户单击客户端上的“导出不带 mailId 的记录”时,我希望能够将数据库中的一些记录导出为 CSV。实现这一目标的最佳方法是什么?在我的研究中,我发现人们只使用 Servlet 作为示例,但对我来说,我不使用 Servlet。
这是我的 Controller :
@RequestMapping(value = "/eblnotif/exportMailId", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody List<EblNotifResource> exportMailIdCsv(@RequestBody Filters filters)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, IOException {
List<EblNotif> eblnotif_list = accountSservice.exportMailIdCsv(filters);
List<EblNotifResource> eblnotifres = new ArrayList<>();
for (EblNotif eblNotif : eblnotif_list) {
EblNotifResource eblres = new EblNotifResource();
eblres.setFormName(eblNotif.getFormName());
eblres.setSendingDate(eblNotif.getSendingDate());
eblres.setMode(eblNotif.getMode());
eblres.setLanguage(eblNotif.getLanguage());
eblres.setGpart(eblNotif.getGpart());
eblres.setEmail(eblNotif.getEmail());
eblres.setMailId(eblNotif.getMailId());
eblnotifres.add(eblres);
}
return eblnotifres;
}
}
并将此 json 格式返回给客户端(JSP):
[
{
"formName":"FormName1",
"mode":"S",
"language":"F",
"sendingDate":"2017-04-03",
"gpart":"555",
"email":"",
"mailId":"96318"
},
{
"formName":"FormName2",
"mode":"S",
"language":"F",
"sendingDate":"2017-04-03",
"gpart":"444",
"email":"",
"mailId":"96325"
}
]
csv 中所需的输出将类似于:
formName;mode;language;sendingDate;gpart;email;mailId
FormName1;S;F;2017-04-03;555;testing@test;96318
FormName2;S;F;2017-04-03;444;testing@test;96325
最佳答案
如果仅需要一次性下载 CSV,则在 Controller 方法内转换相关对象可能是最简单的选择。但是,如果多个地方都需要此类下载,则通过 HttpMessageConverter
将 CSV 支持添加到 Spring MVC Rest 可能会更有利。 future 。
Step 1: Optional, but recommended
使用 CSV 转换库,如 OpenCSV , uniVocity或Smooks (仅 CSV 解析器)将对象与 CSV 相互转换。使用好的库将节省大量样板代码,并且还可以用于扩展对制表符分隔值 (TSV) 和固定宽度列等其他格式的支持。
Step 2: Implement
GenericHttpMessageConverter
创建 GenericHttpMessageConverter
的实现使用步骤 1 中选择的 CSV 转换库将对象与 CSV 相互转换。此步骤有点棘手,因为 HttpMessageConverter
通常用于将单个对象转换为所需的内容类型,而 CSV 内容是对象的集合(每行代表一个对象)。然而,Spring 团队在 Jaxb2CollectionHttpMessageConverter
中提供了一个优秀的样本。用于将对象集合转换为所需的内容类型。需要牢记以下几点:
boolean canRead(Class<?>, MediaType)
方法应始终返回false
由于 CSV 内容是对象的集合,因此无法将其读入由类型指定的单个对象。boolean canRead(Type, Class<?>, MediaType)
方法应该检查Type
确保它指定Collection
type 并且还指定了集合应包含的元素类型。T read(Type, Class<?>, HttpInputMessage)
应实现方法来读取 CSV 内容。void write(T, Type, Class<?>, HttpInputMessage)
应实现方法来写入 CSV 内容。
Step 3: The CSV message converter should be registered in the configuration
@Override
protected void configureMessageConverters(final List<HttpMessageConverter<?>> converters)
{
converters.add(csvMessageConverter());
super.addDefaultHttpMessageConverters(converters);
}
Step 4: Controller methods should be made to return a collection of the required objects
public List<?> get() { ... }
<小时/>
示例应用程序已可用 on Github在行动中展示这一点。启动应用程序,然后发送请求到 http://localhost:8080/persons.csv
或http://localhost:8080/persons.json
分别检索 CSV 或 JSON 格式的数据。
关于java - JSP和SPRING启动: Generate CSV file from JSON input?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44088727/