What is the difference between Resource Class and

2019-08-29 10:12发布

问题:

In Jersey/JAX-RS I hear the terms "Resource classes" and "Providers". I am not clear on what the difference between these two are. Can someone please provide some proper explanation?

回答1:

Resource classes are your classes annotated with @Path and providers are any other classes that we can extend/implement that allow us to tap into/extend the JAX-RS framework, i.e.

  • ContainerRequestFilter
  • ContainerResponseFilter
  • MessageBodyReader
  • MessageBodyWriter
  • WriterInterceptor
  • ReaderInterceptor
  • ContextResolver
  • ExceptionMapper
  • ClientRequestFilter
  • ClientResponseFilter

Aside from the client filters, all of these provider classes can be annotated with @Provider and automatically register on the server side when scanning is enabled.

You might often hear questions like "What JSON provider are you using?" This refers to the MessageBodyReader or MessageBodyWriter, which is a type of provider, or more precisely an "Entity Provider", as mentioned in the docs. This is really the only provider type with its own prefix type. All others are just generalized as being a "Provider".

There may be more provider types, but these are all I can think of off the top of my head right now.