简体   繁体   English

Java REST服务PUT参数

[英]Java REST service PUT parameters

This is how my Jersey Rest web service's a few methods looks likes, I have some methods to update the user settings like: 这是我的Jersey Rest Web服务的几种方法的样子,我有一些方法可以更新用户设置,例如:

@PUT
@Path("/langauge")
@Consumes("text/plain")
public void updateLanguage(String lang) {

    ***check validity of the lang by string comparisons**
     and update database with the new language*
}

@PUT
@Path("/threshold")
@Consumes("text/plain")
public void updateThreshold(Long threshold) {

   *//check value and update in server*
}

Now I have a few questions here; 现在我在这里有几个问题。

1- Instead of different resource paths for different update options, is it better to create one resource and update with query parameters? 1-创建一个资源并使用查询参数进行更新是否更好,而不是针对不同的更新选项使用不同的资源路径? This looks more Restish but I'm not sure I really should change it to something like, because in future if there are more parameters to be updated than it will be very cluttered, while having independent paths looks more clear? 这看起来更让人放松,但我不确定我是否应该将其更改为类似的内容,因为在将来,如果要更新的参数多于混乱的参数,而拥有独立的路径看起来会更清晰?

@PUT
@Path("/settings/{lang}/{threshold}")
@Consumes("text/plain")
public void updateSettings(@PathParam("threshold") String thre,
        @PathParam("lang") String lang,
        @DefaultValue("") @QueryParam) {


}

2- Another question is while updating the language now I accept a language as a string then check if thats valid or not in server, So when client uses this method they don't know what exactly should they send as valid parameters. 2-另一个问题是,现在更新语言时,我接受一种语言作为字符串,然后检查在服务器中是否有效,因此,当客户端使用此方法时,他们不知道应该将什么作为有效参数正确发送。 Is there a way to make this more user friendly, putting some comments on WADL file is an option, but is there another more REST way of doing this? 有没有一种方法可以使它更加用户友好,可以在WADL文件上添加一些注释,但是还有另一种REST方法吗?

Without knowing the entire scope of your application, I'd say, you could consider the settings a resource and the particular options as the member of the collection of settings. 我想说,在不知道应用程序整个范围的情况下,您可以将设置视为资源,并将特定选项视为设置集合的成员。 This would allow to add new settings (downside is, that a client that wants to update multiple would need to know and call all of them): 这将允许添加新设置(缺点是,想要更新多个设置的客户端需要知道并调用所有设置):

/settings/language
/settings/threshold
...
# later
/settings/timeout
/settings/temperature

You could implement the GET method on each of the options to display information to the interested client (either as HTML or text or some other format). 您可以在每个选项上实现GET方法,以向感兴趣的客户端显示信息(HTML或文本或其他格式)。

Another way would be to collect all settings at one endpoint and to accept a request entity in some format (eg JSON) as a representation of all available settings. 另一种方法是在一个端点收集所有设置,并接受某种格式(例如JSON)的请求实体作为所有可用设置的表示。 This approach can be observed in the wild in the elasticsearch APIs (eg here: http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings.html ). 可以在Elasticsearch API(例如: http//www.elasticsearch.org/guide/reference/api/admin-indices-update-settings.html )中观察到这种方法。

Answers : 答案:

1) Since they are separate resources, they should have separate service methods. 1)由于它们是单独的资源,因此应具有单独的服务方法。 So the earlier approach is great. 因此,较早的方法很棒。 That makes your code flexible. 这使您的代码更灵活。

2) You can give unique IDs to each of your resources (language in this case) and use it to update the language like: 2)您可以为每个资源(在这种情况下为语言)赋予唯一的ID,并使用它来更新如下语言:

api/language/{languageId}

This ID could them be auto-generated through the database or you could define some other mechanism. 这些ID可以通过数据库自动生成,也可以定义其他机制。

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

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