3

In my spring boot application a request comes from the client with the following URL.localhost:8080/api/invoice?invoiceNo=1234&invoiceDate=050218&invoiceNo=6543&invoiceDate=060218

How can I get the values for the request property invoiceNo and invoiceDate. I know we can always use some delimiter while building the URL and fetch it. I would like to know if there is any springboot way for achieving this. Any help is much appreciated. Now when I try request.getParameter("invoiceNo") I get only the first parameter.

4
  • There are two of each, do you need both, or there is some override logic? Commented Sep 25, 2018 at 16:36
  • 2
    Try request.getParameterValues('invoiceNo'); Commented Sep 25, 2018 at 16:39
  • @khachik yes I need both the values. Commented Sep 25, 2018 at 16:45
  • @MaruthiAdithya Thanks that worked ! Commented Sep 25, 2018 at 16:46

3 Answers 3

10

Use List

public void invoice(@RequestParam(name="invoiceNo") List<String> invoiceNos, 
                    @RequestParam(name="invoiceDate") List<String> invoiceDates)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I just posted that answer
3

In spring you can get query parameters by using the annotation @RequestParam inside controller's endpoint method like this:

@GetMapping("/invoice")
public CustomResponse getInvoiceData(
        @RequestParam(value="invoiceNo") List<Long> invoiceNoList,
        @RequestParam(value="invoiceDate", required = false) List<Date> invoiceDateList){
    ...
}

You can see another values that this annotation can get (like required, default, etc..) in the docs

Comments

0

As @maruthi mentioned request.getParameterValues("invoiceNumber") is one way. Another way is to add @RequestParam(value="invoiceNo", required=false) List<String> invoiceNo as the controller method parameter.

1 Comment

if request url looks like this : localhost:8080/api/invoice?invoiceNo=1234,1235 i want to pass mutltiple values in single queryparam ? how to pass multiple values

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.