All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.
public class SubSubNumber {
@GET @Path("{num1}/{num2}")
public String getAdd(@PathParam("num1") int num, @PathParam("num2") int num2)
{ return "" + (num + num2); } }
Entity parameters are briefly explained in the JAX-RS v1.1 specifications:
3.3.2 Parameters
When a resource method is invoked, parameters annotated with @FormParam or one of the annotations listed in section 3.2 are mapped from the request according to the semantics of the annotation. Similar to fields and bean properties:
• The DefaultValue annotation may be used to supply a default value for parameters
• The Encoded annotation may be used to disable automatic URI decoding of parameter values
• Exceptions thrown during construction of parameter values are treated the same as exceptions thrown during construction of field or bean property values, see section 3.2. Exceptions thrown during construction of @FormParam annotated parameter values are treated the same as if the parameter were annotated with @HeaderParam.
3.3.2.1 Entity Parameters
The value of a non-annotated parameter, called the entity parameter, is mapped from the request entity body. Conversion between an entity body and a Java type is the responsibility of an entity provider, see section 4.2.
Resource methods MUST NOT have more than one parameter that is not annotated with one of the above listed annotations.
I have a problem. The answer is the option 4. But in the code of SubSubNumber you are trying to inject two parameters. Ok, but the specification says (3.2)
"A JAX-RS implementation is only required to set the annotated field and bean property values of instances
created by its runtime. Objects returned by sub-resource locators (see Section 3.4.1) are expected to be
initialized by their creator."
So it's not sure that option 4 returns the add of the two numbers.
In fact in the version 2.0 in 9.2.7 there is a clear solution to this issue by mean of ResourceContext.
1 @Path("widgets")
2 public class WidgetsResource {
3 @Context
4 private ResourceContext rc;
5
6@Path("{id}")
7 public WidgetResource findWidget(@PathParam("id") String id) { 8 return rc.initResource(new WidgetResource(id));
9 }
10 }
11
12 public class WidgetResource {
13 @Context
14 private HttpHeaders headers;
15
16 public WidgetResource(String id) {...}
17
18 @GET
19 public Widget getDetails() {...}
"Note that the instance returned by the resource locator findWidget in WidgetsResource is initialized
using the injected ResourceContext before it is returned. Without this step, the headers field
in WidgetResource will not be properly initialized."
A JAX-RS implementation is only required to set the annotated field and bean property values of instances
created by its runtime. Objects returned by sub-resource locators (see Section 3.4.1) are expected to be
initialized by their creator."
So it's not sure that option 4 returns the add of the two numbers.
I have a problem. The answer is the option 4. But in the code of SubSubNumber you are trying to inject two parameters. Ok, but the specification says (3.2)
"A JAX-RS implementation is only required to set the annotated field and bean property values of instances
created by its runtime. Objects returned by sub-resource locators (see Section 3.4.1) are expected to be
initialized by their creator."
So it's not sure that option 4 returns the add of the two numbers.
Good point! I will have to refactor this question. (it seems Jersey v1.1x doesn't follow the specification literally and allows injection on sub-resources.)