About Question enthuware.ocejws.v6.2.185 :

Moderators: Site Manager, fjwalraven

Post Reply
evefuji
Posts: 21
Joined: Fri Apr 11, 2014 8:57 pm
Contact:

About Question enthuware.ocejws.v6.2.185 :

Post by evefuji »

why cannot be @Produces("text/plain")?

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

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

Post by fjwalraven »

Hi !

The Accept header of a browser sends out will have the following elements: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"

which is read as:
1) I prefer "text/html" and "application/xhtml+xml" (q=100% or q=1.0 in this case) , but because "text/html" is mentioned first I will look for a method that has a @Produces of "text/html", if that is not available I will look for a method that has a @Produces of "application/xhtml+xml".

2) If both media types of 1) are not available, I will look for a method that produces "application/xml" (q is 90%), and if that is also not available, I will look for a method that produces all media types "*/*" (q=80%). "text/plain" will fall under the "*/*" category.

In other words if both methods are in the same resource file, JAX-RS will invoke the one that produces "text/html".

Regards,
Frits

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

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

Post by austinor »

I'm copying the entire question and all 4 answer choices below:

Code: Select all

We have AddressConverter RESTful Web Service on the base URL "/SimpleRS/rs" and a root resource with a path of convert. The Web Service returns the Address on the basis of the zip code (zipCode) and the house number (houseNumber). The URL we are requesting from a browser is: 

"http://localhost:8080/SimpleRS/rs/convert?zip=5000&hnr=12"  

Which of the following methods is executed? Assume that all methods are in the same resource class.

A.
@GET 
@Produces("text/plain") 
public String getPText( @QueryParam("zip") int zipCode, @QueryParam("hnr") int houseNumber ) {
    String address = convert( zipCode, houseNumber );
    return address; 
}

B.
@GET 
@Produces("text/html") 
public String getPText( @QueryParam("zip") int zipCode, @QueryParam("hnr") int houseNumber ) {
    String address = convert( zipCode, houseNumber );
    return address; 
}

C.
@GET 
@Produces("application/json") 
public String getPText( @QueryParam("zip") int zipCode, @QueryParam("hnr") int houseNumber ) {
    String address = convert( zipCode, houseNumber );
    return address; 
}

D.
@GET 
public String getPText( @QueryParam("zip") int zipCode, @QueryParam("hnr") int houseNumber ) {
    String address = convert( zipCode, houseNumber );
    return address; 
}
I won't bother to show the correct answer here, but I'd just like to check my understanding of other aspects of the JAX-RS request processing with this question (because I think I have a hunch):

If we remove answer choice B, what would happen? (For completeness' sake, let's add "E. None of the above".)

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

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

Post by fjwalraven »

If we remove answer choice B, what would happen? (For completeness' sake, let's add "E. None of the above".)
Choice D would be the correct answer then.

The algorithm first checks if there is a resource method annotated with @Produces("text/html"), then @Produces("application/xhtml+xml"), @Produces("application/xml"), and finally @Produces("*/*")

The first 3 @Produces are not found, the last one @Produces("*/*") applies to any method that is annotated with @Produces("*/*") or not annotated at all (a method with @GET without a @Produces annotation).

Hence option D would be correct if option B was removed.

Regards,
Frits
n.b. no need to copy the whole question setup here.

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

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

Post by ramy6_1 »

Hello ,

So you mean if the first option available

@GET @Produces("text/plain") public String getPText(@QueryParam("zip") int zipCode,           @QueryParam("hnr") int houseNumber) {    String address = convert(zipCode, houseNumber);    return address; }

And the same HTTP request from the browser.

Browser will return 405 Method not allowed ?

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

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

Post by fjwalraven »

No, just read the explanation to this question.

Regards,
Frits

belenzo
Posts: 1
Joined: Mon Nov 09, 2015 9:21 am
Contact:

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

Post by belenzo »

Hi fjwalraven,

Mabuhay (Greetings from the Philippines)! :D
I answered this incorrectly. :(
I chose:

Code: Select all

@GET 
public String getPText(@QueryParam("zip") int zipCode, @QueryParam("hnr") int houseNumber) {    
    String address = convert(zipCode, houseNumber);    
return address; }
Please confirm, are you saying that when an ACCEPT header is not specified programmatically, the browser will have the ACCEPT header value of "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" by default? Is that what you mean?

Thanks in advanced!
- Jasper

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

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

Post by fjwalraven »

Hi Jasper!
Please confirm, are you saying that when an ACCEPT header is not specified programmatically, the browser will have the ACCEPT header value of "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" by default? Is that what you mean?
Yes, exactly!

Regards,
Frits

sttaq0442
Posts: 27
Joined: Tue Nov 15, 2016 11:20 am
Contact:

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

Post by sttaq0442 »

fjwalraven wrote:Hi Jasper!
Please confirm, are you saying that when an ACCEPT header is not specified programmatically, the browser will have the ACCEPT header value of "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" by default? Is that what you mean?
Yes, exactly!

Regards,
Frits
hmmm this is not true for all browsers. For example Chrome and Safari don't have the same accept header. See here.

So, I think the accept header should be specified in the question.

For the exam, can you confirm if it is safe to assume the header you have mentioned?

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

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

Post by fjwalraven »

hmmm this is not true for all browsers. For example Chrome and Safari don't have the same accept header.
True, the Accept values will differ from browser to browser but all of them will start with "text/html".

current values:
Firefox:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Chrome
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

IE/Edge
text/html, application/xhtml+xml, image/jxr, */*
For the exam, can you confirm if it is safe to assume the header you have mentioned?
Yes, for the exam you will be given the Accept header.

Regards,
Frits

sttaq0442
Posts: 27
Joined: Tue Nov 15, 2016 11:20 am
Contact:

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

Post by sttaq0442 »

fjwalraven wrote:
hmmm this is not true for all browsers. For example Chrome and Safari don't have the same accept header.
True, the Accept values will differ from browser to browser but all of them will start with "text/html".

current values:
Firefox:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Chrome
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

IE/Edge
text/html, application/xhtml+xml, image/jxr, */*
For the exam, can you confirm if it is safe to assume the header you have mentioned?
Yes, for the exam you will be given the Accept header.

Regards,
Frits
Thanks for the clarification

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests