4

I try to pass a parameter via GET to a REST method.

@GET
@Path("{id}")
public Response getUser(@PathParam("id") String id) {
    Query qry = em.createQuery("from User c WHERE id = :user_id");
    qry.setParameter("user_id", id);
    List<User> results = qry.getResultList();

    if(results.size() > 0) {
        return Response.ok(results.get(0),MediaType.APPLICATION_JSON_TYPE).build();
    } else {
        return Response.serverError().status(Response.Status.NOT_FOUND).build();
    }
}

If I call it via the Rest Client with:

client = ClientBuilder.newClient();    
Response response = client.target(TestPortProvider.generateURL("/user")+"/abc").request().get(Response.class);

then the method gets called but the parameter is empty. If I remove the "abc" from the GET url the method gets not called. Also if I remove the @Path("{id}") it doesn't work too. It seems as there is a parameter but it's empty for no reason. Maybe some one can help me to fix the problem.

kind regards

Sesigl
  • 600
  • 4
  • 17

4 Answers4

16

sadly the reason was a wrong import for PathParam. so a big note to my desk ... if unchecked things doesn't work ... check your imports that are generated by your IDE.

Sesigl
  • 600
  • 4
  • 17
6

{} is not required in the method parameter, just give the name. Try the following:

 public Response getUser(@PathParam("id") String id) 
HJK
  • 1,382
  • 2
  • 9
  • 19
4

The import can get defaulted to:

import javax.websocket.server.PathParam;

Replace it with the one from the JAX-RS API:

import javax.ws.rs.PathParam;
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
ASH
  • 980
  • 1
  • 9
  • 22
2

The reason it would have not worked is the wrong import for PathParam. So import the javax.ws.rs.PathParam and check it again.