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?

6
  • Possible duplicate of Java: Sending Multiple Parameters to Method Commented Nov 8, 2019 at 10:44
  • Can you add the URL that you call the method? Commented Nov 8, 2019 at 10:44
  • Here is the url: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" Commented Nov 8, 2019 at 10:48
  • Please add the GET command to the question you use to call the endpoint. Commented Nov 8, 2019 at 10:54
  • 2
    You can check this answer: stackoverflow.com/a/22298768/11733759 Commented Nov 8, 2019 at 10:56

2 Answers 2

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 {
        ...
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.