I am developing a rest server in java, netbeans.
I made my first get method and my class looks like this:
@Stateless
@Path("v1/cardapio")
public class CardapioResource {
private Gson gson = new Gson();
@EJB
private CardapioRemote ejb;
public CardapioResource() {}
@GET
@Produces("application/json")
@Path("/")
public String getCardapios(@QueryParam("key") String key) {
Conta c = ContaDAO.busca(key);
JsonObject obj = new JsonObject();
if(c != null){
JsonArray array = (JsonArray) gson.toJsonTree(ejb.findAll());
obj.add("dados", array);
} else{
JsonObject status = new JsonObject();
status.addProperty("codigo", 401);
status.addProperty("mensagem", "Não há nenhum ID correspondente a este KEY");
obj.add("status", status);
}
return obj.toString();
}
@GET
@Produces("application/json")
@Path("/")
public String getCardapios(@QueryParam("key") String key, @QueryParam("id") String id) {
// second method
}
}
The above method is responsible for validating a fkey in the database and if it is valid returns a list of menus.
So I tried to do the second method, get an id ... and after validated it would return only the menu of the given id. My class looks like this:
@Stateless
@Path("v1/cardapio")
public class CardapioResource {
private Gson gson = new Gson();
@EJB
private CardapioRemote ejb;
public CardapioResource() {}
@GET
@Produces("application/json")
@Path("/")
public String getCardapios(@QueryParam("key") String key) {
// first method
}
@GET
@Produces("application/json")
@Path("/")
public String getCardapios(@QueryParam("key") String key,
@QueryParam("id") String id ) {
Conta c = ContaDAO.busca(key);
JsonObject obj = new JsonObject();
if(c != null){
JsonArray array = (JsonArray) gson.toJsonTree(ejb.findById(Integer.parseInt(id)));
obj.add("dados", array);
} else{
JsonObject status = new JsonObject();
status.addProperty("codigo", 401);
status.addProperty("mensagem", "Não há nenhum ID correspondente a este KEY");
obj.add("status", status);
}
return obj.toString();
}
}
After the inclusion of this second method an exception is occurring :
WebModule[/webPlataformaCardapio]StandardWrapper.Throwable [FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by @Consumes and @Produces annotations at Java methods public java.lang.String ws.CardapioResource.getCardapios(java.lang.String) and public java.lang.String ws.CardapioResource.getCardapios(java.lang.String,java.lang.String) at matching regular expression /. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.; source='org.glassfish.jersey.server.model.RuntimeResource@4fef476']
Imagine I have a server url servidor:porta/api/produtos?key=1 that validates the key and returns all products ... and I can have the servidor:porta/api/produtos?key=1&id=5 that validates the key and returns only product 5. Both have the same path, with no divisions of "/".
How can I solve?