user3573403
user3573403

Reputation: 1802

JAX-RS @PathParam: how to pass anything including "?"

I have the following JAX-RS method with @PathParam.

@GET
@Produces(MediaType.APPLICATION_XML)
@Path("collect/{orderItemID}")
public Response collect(@PathParam("orderItemID") String orderItemID) {
    System.out.println("Order Item ID: " + orderItemID);
    ....
}

I want the method to accept anything that is passed to it. However, when I pass the following string, the container, WebLogic, returned HTTP 404 Not Found. The problem is with the ? in the beginning of the string. If I remove the ?, it will get accepted and the System.out.println() method will print out the value.

?ProductCode=yyy&EntityNo=1234&Name=what&FileReference=xxx

Next I changed the @Path to the following value:

@Path("collect/{orderItemID : (.+)?}")

This time, when I passed the above string with the ?, there is no more HTTP 404 Not Found. However, inside the method, System.out.println() now prints a blank value of orderItemID.

How do I specify @Path so that the method can accept anything?

Upvotes: 0

Views: 624

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208984

What you are trying use in the URL

?ProductCode=yyy&EntityNo=1234&Name=what&FileReference=xxx

is called a query string. This is not something that you try to as a single path parameter. When the URL is retrieved by Jersey, it will parse the URL and extract the query string. It knows it's a query string because of the ?.

Once it has query string, it will parse it, splitting the = and &, and make the key value pairs available in a number of different ways. One way to get them inside of the resource method is just to use @QueryParam. With this annotation, you can use the key, and the value of that key will get passed to the method

public Response collect(@QueryParam("ProductCode") String productCode,
                        @QueryParam("EntityNo") String entityNo,
                        @QueryParam("Name") String name,
                        @QueryParam("FileReference") String fileRef) {}

If you goal is to actually be able to pass anything and you problem is actually about passing a ? (that is not a reference to the query string), when what you need to do is something like

@Path("{anything: .*}")

Then on the client side, you need to URL encode the ?

Upvotes: 1

Related Questions