简体   繁体   中英

How to create/call a rest controller in springboot with both path and request parameter

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM