Hello! This code is taken as an example from the Jersey Guide. I decided to ask a couple of questions in order to understand it.

// Example 3.23. Injection @Path("{id:\\d+}") public class InjectedResource { // Injection onto field @DefaultValue("q") @QueryParam("p") private String p; // Injection onto constructor parameter public InjectedResource(@PathParam("id") int id) { ... } // Injection onto resource method parameter @GET public String get(@Context UriInfo ui) { ... } // Injection onto sub-resource resource method parameter @Path("sub-id") @GET public String get(@PathParam("sub-id") String id) { ... } // Injection onto sub-resource locator method parameter @Path("sub-id") public SubResource getSubResource(@PathParam("sub-id") String id) { ... } // Injection using bean setter method @HeaderParam("X-header") public void setHeader(String header) { ... } } 
  1. The syntactic record of this line is not clear:

     @Path("{id:\\d+}") 

    As far as I know, it is passed to {The parameter itself: id, name, last name, etc ..} Record {id:\\d+} incomprehensible, and I will be glad if anyone can explain.

  2. The value of @DefaultValue in this example, the variable p by default is equal to "q" ?

  3. Near this variable @QueryParam("p") how to understand such a record, why are there two annotations over the field?

  4. This entry is clear public InjectedResource(@PathParam("id") int id) , id attached to the variable id .

  5. This record is not clear public String get(@Context UriInfo ui) ,

    @Context mechanism - why is this annotation used? From the documentation I did not find anything useful for myself.

  6. The following code seems to contain two methods that do the same thing: the first gets resources and the second, on one Get annotation, on the other - no. What is the difference between them?

     // Injection onto sub-resource resource method parameter @Path("sub-id") @GET public String get(@PathParam("sub-id") String id) { ... } // Injection onto sub-resource locator method parameter @Path("sub-id") public SubResource getSubResource(@PathParam("sub-id") String id) { ... } 
  7. In the second example, the purpose of this method is not clear:

     @Context public void setRequest(Request request) { // injection into a setter method System.out.println(request != null); } 
  8. What is the difference between @Context , @QueryParam , @PathParam among themselves? Maybe for some questions banal and simple, but I, as a novice with Jersey, do not understand the difference in purpose.

     // Example 3.26. Example of possible injections: // https://jersey.java.net/documentation/latest/user-guide.html#d0e2789 @Path("resource") public static class SummaryOfInjectionsResource { @QueryParam("query") String param; // injection into a class field @GET public String get(@QueryParam("query") String methodQueryParam) { // injection into a resource method parameter return "query param: " + param; } @Path("sub-resource-locator") public Class<SubResource> subResourceLocator(@QueryParam("query") String subResourceQueryParam) { // injection into a sub resource locator parameter return SubResource.class; } public SummaryOfInjectionsResource(@QueryParam("query") String constructorQueryParam) { // injection into a constructor parameter } @Context public void setRequest(Request request) { // injection into a setter method System.out.println(request != null); } } public static class SubResource { @GET public String get() { return "sub resource"; } } 
  • You dumped a bunch of questions with a request to chew the documentation into one. This is bad practice. - Nofate
  • Clear. Beginner on the server will know. - Maks.Burkov

1 answer 1

To begin with, since the documentation of a specific JAX-RS (Jersey) implementation is not clear, it is worthwhile to read the JAX-RS specification , in which everything is interpreted as fully and unambiguously as possible.

  1. The @Path specifies the path pattern for the resource. In this case, the path consists of a single template parameter named id , which must satisfy the regular expression \\d+ (which corresponds to an integer of arbitrary length).

    Example:

    • path /1234 - satisfies the pattern, the id value will correspond to the value 1234 ;

    • path /abc123 - does not satisfy the pattern, methods of this class will not be called along this path.

    In the future, the template parameter id path can be associated with the argument of any method annotation @PathParam .


  1. Yes, the @DefaultValue annotation sets the default value if it was not received due to other JAX-RS annotations ( PathParam , QueryParam , MatrixParam , CookieParam , FormParam , or HeaderParam ).

  1. The @QueryParam ("p") annotation above the private String p field private String p binds a URL parameter (the one that goes to the URL after the ? Sign) with the name p to this field. If the p parameter in the URL is missing, the value from the @DefaultValue annotation will be used @DefaultValue

    Example:

    • For a request at /12345?p=abc value of the p field will be equal to "abc" .
    • For a query at /12345 value of the p field will be "q" .

  1. Yes, see point 1.

  1. The @Context embeds the values ​​of the various metadata of the current request, guided by the type of the argument or field. What metadata can be implemented is described in chapter 9 of the specification:

    In your particular case, metadata about the path template ( UriInfo ) of the current request will be embedded in the method argument.


  1. Take a closer look: in one case the String returned, in the other - the SubResource . Because in the first case there is the @GET annotation, then the corresponding requests will be processed by the method. In the second case, there is no annotation describing a specific HTTP method, so Jersey will consider the returned SubResource instance as another JAX-RS resource and look for annotations in it.

  1. See point 5.

  1. See points 1, 3, 5. As I mentioned, there are still: MatrixParam , CookieParam , FormParam , or HeaderParam , read about them too.