0

I have variant resources that all extend BaseResource<T>

@Component
@Path("/businesses")
public class BusinessResource extends BaseResource<Business>{

   @GET
   @Path({businessId}/)
   public Business getBusiness(@PathParam("businessId") Integer businessId){..}
}

@Component
@Path("/clients")
public class ClientResource extends BaseResource<Client>{

   @GET
   @Path({clientId}/)
   public Client getClient(@PathParam("clientId") Integer clientId){..}
}

I would like, that when there is a call to /businesses/3, it will first go through a method that I will write which validates the T object and if everything is ok I will tell jersey to continue handling the resource. Same goes for Client.

I can't use a regular servlet/filter - since it's being called BEFORE jersey servlet and I wouldn't know which resource is being called.

What is the best way to do it in Jersey?

Is there a place to interfere between knowing the method that jersey will invoke and the invokation?

Dejell
  • 13,947
  • 40
  • 146
  • 229

2 Answers2

0

There are 4 basic http methods in REST, namly GET, PUT, POST, DELETE. Your annotation tells Jersey what method to call when a http request occurs. Jersey looks up the target URI in the request and matches it against your model. If the request is a http get it will execute the method annotiated with @Get from the class with the correct @Path annotiaton. Usually you dont want to grant access to your resources in this annotated method directly. A common (may not perfect) way is to implement a DAO class that handles access to your resources, and of course does the validation before it returns the resource back to the @Get annotated method, which will itself only pass the resource to the client. So you will get another layer in your application between persisting (SQL, etc) and the client interface (Jersey).

sschrass
  • 7,014
  • 6
  • 43
  • 62
0

You can use jersey 2.x ContainerRequestFilters with NameBinding. After having matched the resource, the bound filter will be executed prior to executing the method itself. You can see the Jersey user guide, which states that it is possible: Chapter 9.2.1.1 explains about PreMatching and PostMatching filters and chapter 9.4 chapter shows the execution order of jersey filters.

See my post for the implementation where I had the problem to make the filters with jersey 2 work.

Community
  • 1
  • 1
ulrich
  • 1,431
  • 3
  • 17
  • 46