0

I am having trouble passing multiple parameters to GET resource in my controller. I have created a named query in my repository. When i call this GET endpoint, it should execute the named query by passing parameters.

Below code should take multiple parameters as input for example ID = 1,2,3,4 etc. It only takes single input as param.

@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();
    }

downloaded zip file should contain multiple ID records which were passed as parameter when calling the GET endpoint.

can someone look into my code and point out what needs changing?

pitsa
  • 93
  • 2
  • 7
  • Possible duplicate of [Java: Sending Multiple Parameters to Method](https://stackoverflow.com/questions/17837117/java-sending-multiple-parameters-to-method) – Ssr1369 Nov 08 '19 at 10:44
  • Can you add the URL that you call the method? – er-han Nov 08 '19 at 10:44
  • Here is the url:http://localhost:8080/v1/message/1&2 and i get below http error: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Fri Nov 08 10:46:12 GMT 2019 There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long[]'; nested exception is java.lang.NumberFormatException: For input string: "1&2" – pitsa Nov 08 '19 at 10:48
  • Please add the GET command to the question you use to call the endpoint. – Boris Nov 08 '19 at 10:54
  • 2
    You can check this answer: https://stackoverflow.com/a/22298768/11733759 – lczapski Nov 08 '19 at 10:56
  • Thank you. My code works. i was using a wrong URL – pitsa Nov 08 '19 at 11:02

2 Answers2

0

You achieve the multiple input parameters in POST request method.

In the request payload, please add this array of integer in your Request payload.

[1,2,3,4,5]

To achieve same thing in GET request method convert your array of integer into string.

Example:

localhost:8080/user/str=1,2,3
GnanaJeyam
  • 2,780
  • 16
  • 27
0

You can rewrite it to List of Ids - `List Ids

@GetMapping("/message/{Ids}")
    @CrossOrigin(origins = "*")
    public void multidownload(@PathVariable List<Long> Ids , HttpServletResponse response)throws Exception {
        ...
ErikHer
  • 227
  • 2
  • 5
  • 12