简体   繁体   English

如何使用“?”没有得到路径休息?

[英]How to use “?” no get Path Rest?

I am developing a rest server in java, netbeans. 我正在用Java,Netbeans开发一个Rest服务器。 I have my GET request: 我有我的GET请求:

//myip/application/v1/cardapio/id=1 // myip / application / v1 / cardapio / id = 1

@Stateless
@Path("v1/cardapio")
public class CardapioResource {
        @GET
        @Produces("application/json")
        @Path("id={id}")
        public String getCardapio(@PathParam("id") int id) {

            JsonArray array = (JsonArray) gson.toJsonTree(ejb.findById(id));
            JsonObject obj = new JsonObject();
            obj.add("dados", array);
            return obj.toString();
        }
}

It works correctly. 它可以正常工作。

But I want to do differently, as I saw in other examples, I want to mark the beginning of the variables with the "?". 但是我想做不同的事情,正如我在其他示例中看到的那样,我想用“?”标记变量的开头。

Ex: //myip/application/v1/cardapio/?id=1 例如:// myip / application / v1 / cardapio /?id = 1

    @Stateless
    @Path("v1/cardapio")
    public class CardapioResource {
            @GET
            @Produces("application/json")
            @Path("?id={id}")
            public String getCardapio(@PathParam("id") int id) {

                JsonArray array = (JsonArray) gson.toJsonTree(ejb.findById(id));
                JsonObject obj = new JsonObject();
                obj.add("dados", array);
                return obj.toString();
            }
    }

Thus error 404, page not found. 因此出现错误404,找不到页面。

You can't, after ? 你不能吗? sign it's query parameters and not path parameters 签名它的查询参数而不是路径参数

You can use @QueryParam("id") 您可以使用@QueryParam("id")

What you seen in "other examples" is just normal usage of URL's query part. 您在“其他示例”中看到的只是URL查询部分的正常用法。 Just use it with @Queryparam 只需将其与@Queryparam一起@Queryparam

   @Stateless
    @Path("v1/cardapio")
    public class CardapioResource {
            @GET
            @Produces("application/json")
            @Path("/") // can be removed actually
            public String getCardapio(@QueryParam("id") int id) {

                JsonArray array = (JsonArray) gson.toJsonTree(ejb.findById(id));
                JsonObject obj = new JsonObject();
                obj.add("dados", array);
                return obj.toString();
            }
    }

Here you are mapping getCardapio to v1/cardapio/ and you will try to get id from query string so 在这里,您将getCardapio映射到v1/cardapio/并且您将尝试从查询字符串获取id ,因此

Ex: //myip/application/v1/cardapio/?id=1

will just work. 会工作。

您还可以使用@RequestParam("id") int id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM