首页 > 解决方案 > What is the correct restful API design when we have optional behavior?

问题描述

I have use case where a class of students is broken down into teams. class has a classId and team has a teamId.

I have the following options:

  1. Create 2 endpoints:

    @GET
    @Path("/schoolMetadata/byClass/{classId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getMetadataByClassId(
    

and

    @GET
    @Path("/schoolMetadata/byTeam/{teamId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getMetadataByTeamId(
  1. Second implementation would be using query param.

    @GET
    @Path("/schoolMetadata/by/")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getMetadataById(
        @QueryParam("classId") final Id classId
        @QueryParam("teamId") final Id groupId) {
        if (classid != null) {
        }  else {
        }
    }
    

Which of the 2 approaches is better ?

标签: javarestapi

解决方案


I guess the first approach is better because it fits the Single Responsibility Principle (SOLID).

The single responsibility principle (SRP) asserts that a class or module should do one thing only. Now, this is kind of subjective, so the principle is reinforced with the heuristic that the class or module should have only one reason to change.

1 approach has two methods (modules) and each do only one thing


推荐阅读