我在将多个参数传递给 Controller 中的 GET 资源时遇到问题。我在我的存储库中创建了一个命名查询。当我调用此 GET 端点时,它应该通过传递参数来执行命名查询。
下面的代码应该接受多个参数作为输入,例如 ID = 1,2,3,4 等。它只接受单个输入作为参数。
@GetMapping("/message/{Ids}")
@CrossOrigin(origins = "*")
public void multidownload(@PathVariable Long[] Ids , HttpServletResponse response)throws Exception {
List<MessageRepository> messageRepository = Repository.findbyId(Ids);
String xml = new ObjectMapper().writeValueAsString(messageRepository);
String fileName = "message.zip";
String xml_name = "message.xml";
byte[] data = xml.getBytes();
byte[] bytes;
try (ByteOutputStream bout = new ByteOutputStream();
ZipOutputStream zout = new ZipOutputStream(bout)) {
zout.setLevel(1);
ZipEntry ze = new ZipEntry(xml_name);
ze.setSize(data.length);
zout.putNextEntry(ze);
zout.write(data);
zout.closeEntry();
bytes = bout.getBytes();
}
response.setContentType("application/zip");
response.setContentLength(bytes.length);
response.setHeader("Content-Disposition", "attachment; " + String.format("filename=" + fileName));
ServletOutputStream outputStream = response.getOutputStream();
FileCopyUtils.copy(bytes, outputStream);
outputStream.close();
}
下载的 zip 文件应包含多个 ID 记录,这些记录在调用 GET 端点时作为参数传递。
有人可以查看我的代码并指出需要更改哪些内容吗?
最佳答案
您可以将其重写为 Id 列表 - `List Ids
@GetMapping("/message/{Ids}")
@CrossOrigin(origins = "*")
public void multidownload(@PathVariable List<Long> Ids , HttpServletResponse response)throws Exception {
...
关于java - 如何在 Controller 中传递多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58765005/