java - 如何解决GETJson口是心非异常?

标签 java json

我正在用 java、netbeans 开发一个休息服务器。

我创建了第一个 get 方法,我的类如下所示:

@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
    }
}

上述方法负责验证数据库中的 fkey,如果有效则返回菜单列表。

所以我尝试执行第二种方法,获取一个 id ...并且在验证后它只会返回给定 id 的菜单。我的类(class)如下所示:

@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();
    }
}

包含第二个方法后,发生异常:

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']

假设我有一个服务器 URL servidor:porta/api/produtos?key=1 来验证 key 并返回所有产品...并且我可以拥有 servidor:porta/api/produtos?key=1&id=5验证 key 并仅返回产品 5。两者具有相同的路径,没有“/”分隔。

如何解决?

最佳答案

您的错误消息很明确:

These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail

您监听相同的路径、相同的 HTTP 方法和输入/输出的 MIME 类型。

您需要使它们不同,以便您的服务器可以清楚地决定调用哪个方法

例如,您可以将 id 添加到第二个方法的路径。

@GET
@Produces("application/json")
@Path("/id")
public String getCardapios(@QueryParam("key") String key, 
                           @QueryParam("id") String id   ) {
...
}

但是,如果您只想拥有一条路径,则可以创建两个业务方法,一个仅处理 key,另一个处理 keyid

private void businessMethod1(String key) {
    // do your stuff
}

private void businessMethod2(String key, String id) {
    // do your stuff
}

@GET
@Produces("application/json")
@Path("/")
public String getCardapios(@QueryParam("key") String key, 
                           @QueryParam("id") String id   ) {
    if(id == null) {
        businessMethod1(key);
    } else {
        businessMethod2(key, id);
    }
}

关于java - 如何解决GETJson口是心非异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57418322/

相关文章:

json - 使用 PowerShell 将 Chrome 书签导出到 CSV 文件

asp.net-mvc - Controller 总是从 json 仅接收指定字段的 null

java - java可以检测Windows 7通知吗

java - 在 Set 上运行两个循环

java - Spring Boot 下拉菜单

javascript - 在引号内分隔文本?

java - 什么是10001st质数?为什么我的Java解决方案会给出错误ArrayIndexOutOfBoundsException?

java - Android:如何让用户单击按钮来创建(而不是转到)新 Activity

json - Darkcoin nomp 矿池支付处理错误且缺少 ui

c# - 在 C# 中将 PSObjects(或 PS 命令)转换为 JSON