JAX-RS Parameter Annotations - Quiz
Total: 38 questions
1. What the @QueryParam is used for?
2. Which value is set to max method parameter if the request URI is /exams?public=true ?
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getExams(
@DefaultValue("3") @QueryParam("max") int max,
@DefaultValue("true") @QueryParam("public") boolean isPublic) {
...
}
Which value is set to max method parameter if the request URI is /exams?public=true ?
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getExams(
@DefaultValue("3") @QueryParam("max") int max,
@DefaultValue("true") @QueryParam("public") boolean isPublic) {
...
}
max=3
3. Which value is set to max method parameter if the request URI is /exams?max=yyy ?
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getExams(
@DefaultValue("3") @QueryParam("max") int max,
@DefaultValue("true") @QueryParam("public") boolean isPublic) {
...
}
Which value is set to max method parameter if the request URI is /exams?max=yyy ?
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getExams(
@DefaultValue("3") @QueryParam("max") int max,
@DefaultValue("true") @QueryParam("public") boolean isPublic) {
...
}A HTTP 404 (Not Found) response is returned.
4. Which value is set to max and isPublic method parameters if the request URI is /exams ?
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getExams(
@QueryParam("max") int max,
@QueryParam("public") boolean isPublic) {
...
}
Which value is set to max and isPublic method parameters if the request URI is /exams ?
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getExams(
@QueryParam("max") int max,
@QueryParam("public") boolean isPublic) {
...
}max=0
isPublic=false
5. The value of method parameters if the @DefaultValue is not used with @QueryParam and the query parameter are absent in the request?
The value of method parameters if the @DefaultValue is not used with @QueryParam and the query parameter are absent in the request?
The value will be an empty collection for List, Set or SortedSet, null for other object types, and the Java-defined default for primitive types.
6. What @MatrixParam is used for?
What @MatrixParam is used for?
It extracts information from URL path segments (; is used as a delimiter instead of ?).
7. What @HeaderParam is used for?
8. What @CookieParam is used for?
9. What @FormParam is used for?
What @FormParam is used for?
It extracts information from a request representation that is of the MIME media type "application/x-www-form-urlencoded" and conforms to the encoding specified by HTML forms.
10. Which URI can be used to call getExam() method?
@Path("exams")
public class ExamService {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getExam(@MatrixParam("max") int max) {
...
}
}
Which URI can be used to call getExam() method?
@Path("exams")
public class ExamService {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getExam(@MatrixParam("max") int max) {
...
}
}/exams;max=3
11. Which elements injection can be performed on?
12. Restrictions for injecting to resource classes with a life-cycle of singleton scope.
Restrictions for injecting to resource classes with a life-cycle of singleton scope.
The class fields, constructor parameters and setter methods cannot be injected with request specific parameters.
13. Is it valid example?
@Path("exams")
@Singleton
public class ExamResource {
@QueryParam("query")
String param;
@GET
public String get() {
return "query param: " + param;
}
}
Is it valid example?
@Path("exams")
@Singleton
public class ExamResource {
@QueryParam("query")
String param;
@GET
public String get() {
return "query param: " + param;
}
}No, initialization of application will fail as it is impossible to inject request specific parameters into a singleton resource.
14. Which specific request objects can be injected on the class fields, constructor parameters and setter methods in singleton?
Which specific request objects can be injected on the class fields, constructor parameters and setter methods in singleton?
HttpHeaders, Request, UriInfo, SecurityContext.
15. How request objects HttpHeaders, Request, UriInfo, SecurityContext proxies are injected in the singleton?
16. Example of injection of proxies into singleton.
Example of injection of proxies into singleton.
@Path("exams")
@Singleton
public static class SingletonResource {
@Context
Request request;
public SingletonResource(@Context SecurityContext securityContext) {
// ...
}
@GET
public String get() {
return "query param: " + param;
}
}
17. What class fields injection type is used for?
18. Requirements for class fields injection type.
19. Can class fields injection types be used in Singleton scope?
Can class fields injection types be used in Singleton scope?
No, except proxiable types HttpHeaders, Request, UriInfo, SecurityContext.
20. How constructor parameters injection type is used?
21. Can Constructor parameters injection type be used with singleton?
Can Constructor parameters injection type be used with singleton?
No, except proxiable types HttpHeaders, Request, UriInfo, SecurityContext.
22. When resource methods and sub resource locators injection type is used?
When resource methods and sub resource locators injection type is used?
When there are parameters which can be injected when the method is executed.
23. Can resource methods injection types be used with singleton?
Can resource methods injection types be used with singleton?
Yes.
24. Can sub resource locators injection types be used with singleton?
Can sub resource locators injection types be used with singleton?
Yes.
25. What setter methods injection type is used for?
What setter methods injection type is used for?
To initialize the field.
26. Which annotation should be used with setter method injection?
Which annotation should be used with setter method injection?
@Context annotation.
27. When the setters are invoked?
When the setters are invoked?
After the object is created and only once.
28. Should the name of the setter method follow setter pattern?
Should the name of the setter method follow setter pattern?
No, it isn't necessary.
29. Can setter method injection type be used in singleton scope?
Can setter method injection type be used in singleton scope?
No, except proxiable types HttpHeaders, Request, UriInfo, SecurityContext.
30. Is it valid injection into a class field?
@Path("exam")
public class ExamResource {
@QueryParam("query")
private String param;
...
}
Is it valid injection into a class field?
@Path("exam")
public class ExamResource {
@QueryParam("query")
private String param;
...
}Yes.
31. Is it valid injection into a resource method parameter?
@Path("exams")
public class ExamResource {
@GET
public String get(@QueryParam("query") String queryParam) {
return "query param: " + queryParam;
}
...
}
Is it valid injection into a resource method parameter?
@Path("exams")
public class ExamResource {
@GET
public String get(@QueryParam("query") String queryParam) {
return "query param: " + queryParam;
}
...
}Yes.
32. Is it valid injection into a sub resource locator parameter?
@Path("exams")
public class ExamResource {
@Path("categories")
public Class<CategoryResource> getCategories(@QueryParam("query") String param) {
return CategoryResource.class;
}
...
}
public class CategoryResource {
@GET
public String getCategory() {
...
}
}
Is it valid injection into a sub resource locator parameter?
@Path("exams")
public class ExamResource {
@Path("categories")
public Class<CategoryResource> getCategories(@QueryParam("query") String param) {
return CategoryResource.class;
}
...
}
public class CategoryResource {
@GET
public String getCategory() {
...
}
}Yes.
33. Is it valid injection into a constructor parameter?
@Path("exams")
public class ExamResource {
public ExamResource(@QueryParam("query") String queryParam) {
...
}
...
}
Is it valid injection into a constructor parameter?
@Path("exams")
public class ExamResource {
public ExamResource(@QueryParam("query") String queryParam) {
...
}
...
}Yes.
34. Is it valid injection into a setter method?
@Path("exams")
public class ExamResource {
@Context
public void setRequest(Request request) {
...
}
...
}
Is it valid injection into a setter method?
@Path("exams")
public class ExamResource {
@Context
public void setRequest(Request request) {
...
}
...
}Yes.
35. Where a @FormParam annotation can be used?
Where a @FormParam annotation can be used?
On resource and sub-resource methods.
36. What @PathParam is used for?
What @PathParam is used for?
It gives the possibility to inject the value of named URI path parameters that were declared in @Path expressions.
37. Write example of uri for this example:
@Path("exams")
public class ExamService {
@GET
@Produces( "application/xml")
public String get( @QueryParam("number") int number,
@QueryParam("language") String language) {
...
}
}
Write example of uri for this example:
@Path("exams")
public class ExamService {
@GET
@Produces( "application/xml")
public String get( @QueryParam("number") int number,
@QueryParam("language") String language) {
...
}
}GET /exams?number=10&language=English
38. Example of accessing the HTTP Referer header using the @HeaderParam annotation.
Example of accessing the HTTP Referer header using the @HeaderParam annotation.
@Path("/exams")
public class ExamService {
@GET
public String getData(@HeaderParam("Referer") String referer) {
...
}
}