如何在控制器中传递多个参数?

我无法将多个参数传递到控制器中的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端点时作为参数传递。

有人可以查看我的代码并指出需要更改的内容吗?

q4023586 回答:如何在控制器中传递多个参数?

您可以在POST请求方法中实现多个输入参数。

在请求有效负载中,请将此整数数组添加到请求有效负载中。

[1,2,3,4,5]

要在GET请求方法中实现同一目的,请将整数数组转换为字符串。

示例:

localhost:8080/user/str=1,3
,

您可以将其重写为ID列表-`List IDs

@GetMapping("/message/{Ids}")
    @CrossOrigin(origins = "*")
    public void multidownload(@PathVariable List<Long> Ids,HttpServletResponse response)throws Exception {
        ...
本文链接:https://www.f2er.com/3138598.html

大家都在问