About Question enthuware.ocejws.v6.2.212 :

Moderators: Site Manager, fjwalraven

Post Reply
fabiolino
Posts: 25
Joined: Wed Jun 24, 2015 7:26 am
Contact:

About Question enthuware.ocejws.v6.2.212 :

Post by fabiolino »

Hi,
for the second incorrect answer

Code: Select all

	public class DivisionJerseyClient {
		public static void main(String[] args) {
			Client cl = Client.create(); 
			WebResource webResource = cl.resource("http://localhost:8080/DivRS/j/div");
			String response = webResource.entity("init").get(String.class);
			System.out.println(response.toString());       } }

i just run the same code but i 've a response status of 404 both with and without entity("entity")

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

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

Post by fjwalraven »

HTTP 404 means that there is no corresponding method found in the resource class.

However the GET to URL "http://localhost:8080/DivRS/j/div" maps to the method init(). It doesn't expect an entity and therefore a 405 is returned. If you don't send an entity the init() method should run and give a response.

Regards,
Frits

fabiolino
Posts: 25
Joined: Wed Jun 24, 2015 7:26 am
Contact:

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

Post by fabiolino »

help me to understand what I did wrong

WS:

Code: Select all

@Path("/categoryservice")
public class CategoryService {
        @GET
	@Path("{string}")
	public String init(@PathParam("string") String p) {
		return "init";
	}
}
CLIENT JERSEY (the context root is jaxrs):

Code: Select all

private static void call() {
		Client cl = Client.create();
		WebResource webResource = cl.resource("http://localhost:8080/jaxrs/categoryservice/");
		String response = webResource.
					entity("init").
					get(String.class);
		System.out.println(response.toString());
		
	}
reponse:
com.sun.jersey.client.urlconnection.URLConnectionClientHandler _invoke
Informazioni: GET method with entity will be most likely replaced by POST, see http://java.net/jira/browse/JERSEY-1161
Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: POST http://localhost:8080/jaxrs/categoryservice/ returned a response status of 404 Not Found


with client CXF response is ok

i ask you the entity("init") method add "init" to url http://localhost:8080/jaxrs/categoryservice/ right ?

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

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

Post by fjwalraven »

i ask you the entity("init") method add "init" to url http://localhost:8080/jaxrs/categoryservice/ right ?
No, when you are sending an entity, it is a POST request instead of a GET request. The entity is sent as a body of the POST request, not as URL parameters (like in a GET request)

Regards,
Frits

fabiolino
Posts: 25
Joined: Wed Jun 24, 2015 7:26 am
Contact:

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

Post by fabiolino »

fjwalraven wrote:
i ask you the entity("init") method add "init" to url http://localhost:8080/jaxrs/categoryservice/ right ?
No, when you are sending an entity, it is a POST request instead of a GET request. The entity is sent as a body of the POST request, not as URL parameters (like in a GET request)

Regards,
Frits
:? why in your question the code below return 405 wheter the request client is POST

Code: Select all

@ApplicationPath("j") 
@Path("div") 
@Stateless public class DivisionResource extends Application {   
	@GET 
        @Path("{num}")   
	public Double square(@PathParam("num") Double n1, Double n2){
	return (n1/n2);   
	}
}

Code: Select all

        public class DivisionJerseyClient {
		public static void main(String[] args) {
			Client cl = Client.create();
			WebResource webResource = cl
					.resource("http://localhost:8080/DivRS/j/div/10");
			Double num = 2.0;
			Double response = webResource.entity(num).get(Double.class);
			System.out.println(response.toString());
		}
	}

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

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

Post by fjwalraven »

Sorry, I am not sure I understand your question, can you explain again?

fabiolino
Posts: 25
Joined: Wed Jun 24, 2015 7:26 am
Contact:

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

Post by fabiolino »

fjwalraven wrote:
i ask you the entity("init") method add "init" to url http://localhost:8080/jaxrs/categoryservice/ right ?
No, when you are sending an entity, it is a POST request instead of a GET request. The entity is sent as a body of the POST request, not as URL parameters (like in a GET request)
Really, i don't understand

Code: Select all

String response = webResource.entity("init").get(String.class);
when you are sending an entity, it is a POST request instead of a GET request ...

but get(String.class) Invoke the GET method

:?: what method will be invoked GET or POST ?

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

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

Post by fjwalraven »

The important thing to understand is that you can't send an entity while invoking a GET method. The exact error probably depends on the implementation, but the bottom line is: no method will be invoked by JAX-RS.

Regards,
Frits

MicNeo
Posts: 5
Joined: Tue May 27, 2014 1:09 pm
Contact:

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

Post by MicNeo »

Hey,

I have another question. Is it even possible to invoke square method and get "good" result? I mean, n2 param is not annotated, so what value it will have if somehow it will be invoked? Is there way to send request for such resource (with not annotated params)?

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

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

Post by fjwalraven »

Hi!
Is it even possible to invoke square method and get "good" result?
In this setup: no there isn't. That is because there is no standard MessageBodyWriter defined for a Double.
n2 param is not annotated, so what value it will have if somehow it will be invoked? Is there way to send request for such resource (with not annotated params)?
It is possible when using Strings only. n2 doesn't have to be annotated if the parameter is sent as an entity.

Try this working example:

Code: Select all

Client cl = Client.create();
WebResource webResource = cl.resource("http://localhost:8080/SimpleRS/j/div/10");
String response = webResource.entity("2").get(String.class);
System.out.println(response.toString());
and the method:

Code: Select all

@ApplicationPath("j")
@Path("div")
@Stateless
public class DivisionResource extends Application {
   @POST
   @Path("{num}")
   public String divString(@PathParam("num") String n1, String n2){
      return "" + (new Integer(n1)/new Integer(n2));
   }
}
Result is:
5
Note that parameter n2 is sent as the body of the post request (entity("2")), and parameter n1 ("10") is sent as part of the URL.

Regards,
Frits

Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests