Just Cat
Just Cat

Reputation: 13

spring multiple endpoints with same request params

I have a RestController.

@RestController
@RequestMapping("/api/children")
public class ChildController {

    private ChildService childService;

    @Autowired
    public ChildController(ChildService childService) {
        this.childService = childService;
    }

    @GetMapping
    public List<ChildDto> list() {
        List<ChildDto> response = childService.list();
        return response;
    }


    @GetMapping(params = "parentId")
    public List<ChildDto> getChildrenByParent(@RequestParam Integer parentId) {
        List<ChildDto> response = childService.getChildrenByParent(parentId);
        return response;
    }

    @GetMapping(params = {"fullName", "age", "parentId"})
    public List<ChildDto> getFilteredChildren(@RequestParam String fullName,
                                              @RequestParam Integer age,
                                              @RequestParam Integer parentId) {
        List<ChildDto> response = childService.getFilteredChildren(fullName, age, parentId);
        return response;
    }
}

In my business logic in service it's should be okay if some param in getFilteredChildren is not sent, but I have an issue when I sent request to

http://localhost:8080/api/children?age=10&parentId=3

it' processed by getChildrenByParent, not by getFilteredChildren

What can I do except sending all params like

http://localhost:8080/api/children?age=10&parentId=3&fullName=

or changing path for some of endpoints

Upvotes: 1

Views: 1940

Answers (2)

SSK
SSK

Reputation: 3766

You need to use the correct mapping. If you have a query string params then use single mapping and based on it's value call the appropriate service method

@GetMapping
    public List<ChildDto> getChildren(@RequestParam(name = "fullName", required = false) String fullName,
            @RequestParam(name = "age", required = false) Integer age,
            @RequestParam(name = "parentId", required = false) Integer parentId) {
        List<ChildDto> response = null;
        if (null == fullName && null == age && null == parentId) {
            response = childService.list();
        } else if (null == fullName && null == age) {
            response = childService.getChildrenByParent(parentId);
        } else {
            response = childService.getFilteredChildren(fullName, age, parentId);
        }
        return response;
    }

Upvotes: 1

vox
vox

Reputation: 440

You are not reaching getFilteredChildren() because by default, the request parameteres are all required. As some are missing, this endpoint is discarded. So, one thing you could try, is making some of the parameters optional:

@GetMapping(params = {"fullName", "age", "parentId"})
public List<ChildDto> getFilteredChildren(@RequestParam(required = false) String fullName,
                                          @RequestParam Integer age,
                                          @RequestParam Integer parentId) {
    List<ChildDto> response = childService.getFilteredChildren(fullName, age, parentId);
    return response;
}

Upvotes: 2

Related Questions