1

I have this rest controller method in springboot

@GetMapping("/cghsHcoSearchText/cityId/{cityId}/hcoName/{hcoName}/treatmentName/{treatmentName}")
    public String cghsHcoSearchText(@PathVariable String cityId, @RequestParam(name = "hcoName", required = false) String hcoName, 
            @RequestParam(name = "treatmentName", required = false) String treatmentName) {
        return "Some Text";
    }

It has one PathVariable and 2 optional Request parameter.

Now when I hit this url with treatmentName = null i get Whitelabel Error Page

http://localhost:8082/cghs/cghsHcoSearchText/cityId/011?hcoName=Guru?

Any help will be appreciated.

2
  • 1
    I see several issues here: 1. Your request params must not appear in the URL. You defined them as path variables, too. Most likely, your URL in the GetMapping should just be /cghsHcoSearchText/cityId/{cityId} 2. Your request URL ends with a question mark. There should only be one question mark in your URL - the one that separates the path from the parameter. Subsequent parameters have to be added using an ampersand (&) Commented Jul 22, 2020 at 15:24
  • @JochenReinhardt Thanks. For such clear explanation. Commented Jul 22, 2020 at 18:01

1 Answer 1

2

We should not specify request param as a placeholder in URL mapping. Only the path params should be mentioned in placeholder. Sharing a code snippet and corresponding URL which will help out in understanding this

@GetMapping("hello/{id}")
    public ResponseEntity<Void> printInfo(@PathVariable("id") String id,
            @RequestParam(required = false, name = "name") String name) {
        System.out.println(id + "   " + name);
        return new ResponseEntity<>(HttpStatus.OK);
    }

Here id comes as a path param and name as a request param which is not mentioned in mapping annotation.

URL would look like

http://localhost:8080/hello/234?name=pappi
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Srijan Mehrota. For clarifying the issue. It worked.

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.