0

I am not so into RESTful web service and I have the following doubt about how to correctly implement RESTful style in my servics working on a Spring Boot application (Spring MVC is the same thing).

So basically I have some controller class like this:

@RestController
@RequestMapping("/RoomMedia")
public class RoomMediaController {

    private static final Logger log = LoggerFactory.getLogger(RoomMediaController.class);

    @Autowired
    private RoomMediaService roomMediaService;

    public RoomMediaController() {
        log.debug("RoomMediaController init");
    }


    @RequestMapping(value = "getAllImagesByRoomId",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<byte[]>> getAllImagesByRoomId(Long roomId) throws DataAccessException {

        log.debug("getAllImagesByRoomId START");

        List<byte[]> result = roomMediaService.getRoomImageListById(roomId);

        log.debug(result.toString());

        log.debug("getAllImagesByRoomId END");

        return ResponseEntity.ok(result);

    }
}

I think that, also if the base idea is RESTful like, it can't be considered a true RESTful WS.

I think that the main problem is related to the endpoint: the getAllImagesByRoomId() method handle HTTP GET request toward URL like this: /RoomMedia/getAllImagesByRoomId?roomId=7

From what I have understand reading some tutorial this is not RESTful style because I have to access to a resource without passing parameter, I have to do someting like this:

/RoomMedia/Images/7

Is it my reasoning correct?

Moreover I think that the previous method is pretty trivial also in the implementation: this method return the list of all the images associated to a room having id=7(it is an application related to hotels).

From what I have understand also its logic is againts RESTful principles.

Is it better organize in the following way?

Doing a GET request like this:

  • /RoomMedia/7/Images/: I have to obtain the list of all the images associated to the room having id=7 as URI (or maybe is better handle a path like /7/RoomMedia/Images/ ?).

  • /RoomMedia/7/Images/1 I obtain the image with the id=1 associated to the room having id=7.

Are al these reasoning correct or am I missing something?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

2 Answers2

0

your assumtions are correct. Which of the mapping that you suggest, is deppending totaly on how your model is. Martin Fowler has a nice article about the Richardson Maturity Model which is sum up as:

  • Level 1 tackles the question of handling complexity by using divide and conquer, breaking a large service endpoint down into multiple resources.
  • Level 2 introduces a standard set of verbs so that we handle similar situations in the same way, removing unnecessary variation.
  • Level 3 introduces discoverability, providing a way of making a protocol more self-documenting.

For your project you could use jhipster because its offer you the best of spring with angular under a REST-ful design.

duderoot
  • 977
  • 1
  • 8
  • 23
0

URLs must not contain a verb. The verb is provided by the method, in your case - surprise, surprise - GET.

ByRoomId is pointless as well, because that's what ?roomId= says.

That leaves AllImages, where All is superfluous.

Which results in the URL /RoomMedia/Images?roomId=7. What's unclear is the relationship between RoomMedia and Images, if there is any. If RoomMedia refers to a room, then your suggestion /RoomMedia/7/Images/ would be correct.

/RoomMedia/7/Images/1 is questionable. It should rather be /Images/1. It's irrelevant in which room the image with the id 1 is in.

When you design a REST API, then the first thing to do is to think about all resources you want to expose and their relationships. After that the URLs will reveal themselves automatically, so to speak.

a better oliver
  • 26,330
  • 2
  • 58
  • 66