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) { ... } } 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.The value of
@DefaultValuein this example, the variablepby default is equal to"q"?Near this variable
@QueryParam("p")how to understand such a record, why are there two annotations over the field?This entry is clear
public InjectedResource(@PathParam("id") int id),idattached to the variableid.This record is not clear
public String get(@Context UriInfo ui),@Contextmechanism - why is this annotation used? From the documentation I did not find anything useful for myself.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) { ... }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); }What is the difference between
@Context,@QueryParam,@PathParamamong 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"; } }