gpt4 book ai didi

java - JSP和SPRING启动: Generate CSV file from JSON input?

转载 作者:行者123 更新时间:2023-12-01 17:56:52 25 4
gpt4 key购买 nike

我做了一些研究,但找不到解决这个问题的方法。当用户单击客户端上的“导出不带 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 , uniVocitySmooks (仅 CSV 解析器)将对象与 CSV 相互转换。使用好的库将节省大量样板代码,并且还可以用于扩展对制表符分隔值 (TSV) 和固定宽度列等其他格式的支持。

Step 2: Implement GenericHttpMessageConverter

创建 GenericHttpMessageConverter 的实现使用步骤 1 中选择的 CSV 转换库将对象与 CSV 相互转换。此步骤有点棘手,因为 HttpMessageConverter通常用于将单个对象转换为所需的内容类型,而 CSV 内容是对象的集合(每行代表一个对象)。然而,Spring 团队在 Jaxb2CollectionHttpMessageConverter 中提供了一个优秀的样本。用于将对象集合转换为所需的内容类型。需要牢记以下几点:

  1. boolean canRead(Class<?>, MediaType)方法应始终返回 false由于 CSV 内容是对象的集合,因此无法将其读入由类型指定的单个对象。
  2. boolean canRead(Type, Class<?>, MediaType)方法应该检查Type确保它指定 Collection type 并且还指定了集合应包含的元素类型。
  3. T read(Type, Class<?>, HttpInputMessage)应实现方法来读取 CSV 内容。
  4. 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.csvhttp://localhost:8080/persons.json分别检索 CSV 或 JSON 格式的数据。

关于java - JSP和SPRING启动: Generate CSV file from JSON input?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44088727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com