两种具有不同查询参数的GET方法 [英] Two GET methods with different query parameters

查看:47
本文介绍了两种具有不同查询参数的GET方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以创建相同的GET URI,但使用不同的查询参数吗?

Could we create the same GET URI but with different query parameters?

例如,我有两个REST GET URI:

For example, I have two REST GET URIs:

/questions/ask/?type=rest
/questions/ask/?byUser=john

现在,REST服务无法将两个GET方法识别为单独的,而仅将其声明为第一个GET方法.

Now the REST service is not recognizing two GET methods as separate and considering it only 1 GET method which is declared as first.

  1. 为什么会这样?
  2. 有什么办法可以使两个具有不同查询参数的GET方法?

如果您能引用任何资源,将不胜感激.

It would be highly appreciated if you could quote any resource.

推荐答案

因为资源由其 PATH (而不是其参数)唯一标识.您定义的两个资源具有相同的PATH.

Because a resource is uniquely identified by its PATH (and not by its params). Two resources you define have the same PATH.

@Path("/questions/ask")

根据 JSR-311规范:

这类方法称为子资源方法,被视为常规方法资源方法(请参阅第3.3节),仅调用方法用于与通过串联创建的URI模板匹配的请求URI资源类的URI模板与资源类的URI模板方法.

由于您的数据模型包含两种截然不同的资源,所以我建议使用两种不同的路径创建其他休息方法:

Since your data model includes two distinct resources I suggest making two rest methods with different paths:

@Path("/questions/ask/type")
@Path("/questions/ask/user")

这是RESTful方式,因为一个URI表示一个且仅一个资源,因此不应有任何重载.如果一个URI代表不止一种资源,则意味着您在某处错了.

This is the RESTful way, since one URI represents one and only one resource and there should be no overloading. If one URI represents more than one resource that means you got it wrong somewhere.

这篇关于两种具有不同查询参数的GET方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆