techcurious
techcurious

Reputation: 31

REST Best practices

For REST service is it a best practice to accept the complete Request as input to your service (as below - Option- A) or should we accept individual parameters as in option - B:

Option - A:

 @GET
        @Produces("text/plain")
        public String hello(@Context HttpServletRequest req) {
    }

Option - B

 @GET
        @Produces("text/plain")
        public String hello(@PathParam("id") int id, @BeanParam etc.) {
    }

Upvotes: 1

Views: 63

Answers (2)

Anush
Anush

Reputation: 41

A rest resource is generally identified by its name and the path parameters(optional).

Example : /tweets/{123} - This indicates 'Give me a tweet from the tweets collection identified by its id(123)'

The rest style of design might not mandate the service provider to expose the resource through path/query parameters but the general idea is that the path parameters uniquely identify a resource and query parameters are to be used as filters.

In your case, the obvious choice would be is to go for path parameters but you should use the injected request object only for the purpose of keeping track of things like remote user, session id etc.. which would not be used to identify the resource but something which will be used internally.

Upvotes: 0

Thierry Templier
Thierry Templier

Reputation: 202256

I think it's better to leverage support provided by JAX-RS to extract content from request than working directly on the request object. This will remove some technical plumbing and you will be able to focus on your own processing.

Moreover the Servlet API isn't really REST oriented. I mean, for example, regarding path variables / parameters, you need to extract them by your own.

Hope it helps you, Thierry

Upvotes: 2

Related Questions