About Question enthuware.ocejws.v6.2.195 :

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.

Moderators: Site Manager, fjwalraven

Post Reply
rkbansal83
Posts: 33
Joined: Sat Nov 24, 2012 8:52 am
Contact:

About Question enthuware.ocejws.v6.2.195 :

Post 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);    } }

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.195 :

Post 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

austinor
Posts: 41
Joined: Mon Oct 27, 2014 11:35 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.195 :

Post 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.

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.195 :

Post 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

ramy6_1
Posts: 124
Joined: Wed Feb 12, 2014 2:44 am
Contact:

Re: About Question enthuware.ocejws.v6.2.195 :

Post by ramy6_1 »

Hello ,

Second option will work here if SubNumber constructor be public instead of private - Correct ?

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.195 :

Post by fjwalraven »

Just try and see ;)

Regards,
Frits

ashirvad
Posts: 2
Joined: Mon Feb 15, 2016 12:14 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.195 :

Post by ashirvad »

Hi,

I am not clear with Option 3) "the SubNumber classes cannot be added".

why so ?

Thanks in Advance.

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.195 :

Post 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

mbejar
Posts: 2
Joined: Tue Jun 14, 2016 5:43 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.195 :

Post 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.

mbejar
Posts: 2
Joined: Tue Jun 14, 2016 5:43 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.195 :

Post 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."

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.195 :

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests