Best way to returns a group by response in spring

I have an object that returns me

[
  {
    id: 123
    name: 'foo'
    date: 2019-09-08
  },
  {
    id: 124
    name: 'foo2'
    date: 2019-09-08
  },
  {
    id: 125
    name: 'foo3'
    date: 2019-09-09
  },
  {
    id: 126
    name: 'foo4'
    date: 2019-09-09
  }
]

Which is the best way to configure an endpoint that gives me these objects grouped by date using Spring Boot?

How can I set a good endpoint name for this feature?

I'm thinking about something like:

@GetMapping(value = "/somethings")
public List<Something> getSomethings(@RequestParam(value = "groupBy", required = false) String groupBy)

Is it a good approach?

1 answer

  • answered 2019-09-10 14:38 user12047085

    Returning the grouped information in a list would not work. You can use a Map with key as the field that you are using to group the list by and value as a list that belongs to that particular group.

    For e.g:

    Map<Date,List<SomeThing>> 
    

    Hope this helps