javajax-rsjava-ee-6

URI path template variable in JAX-RS


In JavaEE documentation for JAX-RS webservices, I came across below statement:

http://docs.oracle.com/javaee/6/tutorial/doc/gilik.html

If the URI path template variable cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 400 (“Bad Request”) error to the client. If the @PathParam annotation cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 404 (“Not Found”) error to the client.

@Path("/{username}")
public class MyResourceBean {
    ...
    @GET
    public String printUsername(@PathParam("username") String userId) {
        ...
    }
}

So if request has parameter "username" and if it cannot be type-casted to String then we get 400 error, then when we will get 404 error? I am new to web-services, please help me in understanding this.


Solution

  • Let's say you had something like this instead:

    @Path("/{userId}")
    public class MyResourceBean {
        ...
        @GET
        public String printUsername(@PathParam("userId") int userId) {
            ...
        }
    }
    

    if the URI for your request is something like /abc, then you'd get a 400 because abc cannot be cast into an int. Now, if your request URI is '/', you'll get a 404 because there's no resource method associated with this URI.