سؤال

I have 2 different URLs :

GET /stuff/{id} (where id is an Integer)
GET /stuff/foo?bar={someValue} (foo is not an Integer, it is a hard coded String)

When calling /stuff/foo&bar=someValue, I have the following error:

Failed executing GET /stuff/foo&bar=someValue
...
Caused by: java.lang.NumberFormatException: For input string: "foo&bar=someValue"

The code is :

@GET
@Path("/stuff/{id}")
public Response getById(@PathParam("id") int id) {
    // code
}

@GET
@Path("/stuff/foo")
public Response foo(@QueryParam("bar") String bar) {
    // code
}

I am using RESTEasy and I would like to keep my URLs that way if possible. Obviously RESTEasy just tries the getById method where it should use the foo method.

How can I do to make this work in RESTEasy ? ("change your URLs" is not an answer without detailed explanations about RESTEasy limitations). I tried (to be sure) putting foo code before getById but I had the same error (of course).

Is there any priority notion between declared URLs ?

As a note : I already implemented that kind of URLs in another framework (python-flask) and it worked just fine : you just have to be careful to declare /stuff/foo before /stuff/{id} (specific case before a more generic one).


EDIT: I just made a stupid mistake ! I was calling /stuff/foo&bar=someValue where I should be calling /stuff/foo?bar=someValue. Thanks @Scobal for pointing it out !

هل كانت مفيدة؟

المحلول

You are calling GET /stuff/foo&bar=someValue

You should be calling GET /stuff/foo?bar=someValue

RESTEasy is trying to parse foo&bar=someValue as the {id} field.

I can't give you an answer about RESTEasy URL priorities but you could do this:

@GET
@Path("/stuff/{id}")
public Response getById(@PathParam("id") String id, @QueryParam("bar") String bar) {           
    try { 
        int intId = Integer.parseInt(id);
        // do int id things
    } catch(NumberFormatException e) { 
        // do foo + bar things
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top