Page 1 of 1

About Question enthuware.ocejws.v6.2.212 :

Posted: Wed Jul 01, 2015 10:19 am
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")

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

Posted: Wed Jul 01, 2015 3:05 pm
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

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

Posted: Thu Jul 02, 2015 3:15 am
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 ?

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

Posted: Thu Jul 02, 2015 3:28 am
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

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

Posted: Thu Jul 02, 2015 4:14 am
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());
		}
	}

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

Posted: Thu Jul 02, 2015 4:45 am
by fjwalraven
Sorry, I am not sure I understand your question, can you explain again?

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

Posted: Tue Jul 07, 2015 3:32 am
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 ?

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

Posted: Tue Jul 07, 2015 6:43 am
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

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

Posted: Sat Feb 20, 2016 11:17 am
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)?

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

Posted: Sun Feb 21, 2016 2:43 am
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