Page 1 of 1
About Question enthuware.ocejws.v6.2.195 :
Posted: Sun Apr 13, 2014 8:08 am
by rkbansal83
what should be URL to access this resource ?
will it be something like /addNumbers/5/9 Or /addNumbers/add/5/9
As mentioned in the answer , Correct option is below logic.
Code: Select all
public class AdditionService extends Application
{ @Path("/addNumbers/")
public SubNumber getNumber()
{ return new SubNumber(); } ... }
Code: Select all
public class SubNumber {
@Path("add") public SubSubNumber getAddition(){ return new SubSubNumber(); } }
Code: Select all
public class SubSubNumber {
@GET @Path("{num1}/{num2}")
public String getAdd(@PathParam("num1") int num, @PathParam("num2") int num2)
{ return "" + (num + num2); } }
Re: About Question enthuware.ocejws.v6.2.195 :
Posted: Sun Apr 13, 2014 2:14 pm
by fjwalraven
Yes, one of the two, not really relevant to the question as it only states
"Assume that two numbers are provided in the URL."
This question is meant to be a real brainer and in those kind of questions we try confuse you sometimes... but it seems you didn't fall for it
Regards,
Frits
Re: About Question enthuware.ocejws.v6.2.195 :
Posted: Mon Nov 03, 2014 8:48 am
by austinor
The last sentence in the explanation says:
"... Sub-resource locators may have all the same parameters as a normal resource method except that they MUST NOT have an entity parameter."
I must have missed the lesson on entity parameters, but what did you mean by the sentence quoted above?
Thank you in advance for the explanation.
Re: About Question enthuware.ocejws.v6.2.195 :
Posted: Mon Nov 03, 2014 2:59 pm
by fjwalraven
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.
Regards,
Frits
Re: About Question enthuware.ocejws.v6.2.195 :
Posted: Tue Nov 10, 2015 9:10 am
by ramy6_1
Hello ,
Second option will work here if SubNumber constructor be public instead of private - Correct ?
Re: About Question enthuware.ocejws.v6.2.195 :
Posted: Wed Nov 11, 2015 7:59 am
by fjwalraven
Just try and see
Regards,
Frits
Re: About Question enthuware.ocejws.v6.2.195 :
Posted: Sun Mar 20, 2016 6:05 am
by ashirvad
Hi,
I am not clear with Option 3) "the SubNumber classes cannot be added".
why so ?
Thanks in Advance.
Re: About Question enthuware.ocejws.v6.2.195 :
Posted: Sun Mar 20, 2016 7:41 am
by fjwalraven
Hi!
in option 3 you will see the following statement:
Code: Select all
return (new SubNumber(num) + new SubNumber(num2));
You just can't add java-classes.
Regards,
Frits
Re: About Question enthuware.ocejws.v6.2.195 :
Posted: Thu Jun 30, 2016 10:22 am
by mbejar
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.
Re: About Question enthuware.ocejws.v6.2.195 :
Posted: Thu Jun 30, 2016 10:26 am
by mbejar
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."
Re: About Question enthuware.ocejws.v6.2.195 :
Posted: Fri Jul 01, 2016 1:13 am
by fjwalraven
Hi!
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.)
Thanks for your feedback!
Regards,
Frits