gpt4 book ai didi

java - 如何使用 Dropwizard 解析 RESTful API 参数

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:04:10 27 4
gpt4 key购买 nike

假设我有:

@GET
public UserList fetch(@PathParam("user") String userId) {
// Do stuff here
}

现在,假设我有自己的 userId 类型,我们称它为 UserId。是否可以在将 String 传递给 fetch 方法时将其解析为 UserId,即:

@GET
public UserList fetch(@PathParam("user") UserId userId) {
// Do stuff here
}

我意识到一旦我进入方法内部就可以解析字符串,但如果我的方法获得我想要的类型会更方便。

最佳答案

好吧,我发现您尝试使用请求正文进行 GET 调用不是很有帮助。请阅读保罗的 answer here -

you can send a body with GET, and no, it is never useful to do so

练习 PUT 或 POST 调用 ( PUT vs POST in REST) 的好处如下 -

@POST
@Path("/some-path/{some-query-param}")
public Response getDocuments(@ApiParam("user") UserId userId,
@PathParam("some-query-param") String queryParam) {
UserId userIdInstance = userId; // you can use the request body further

注意 - 使用的ApiParam 注释是从com.wordnik.swagger.annotations 包导入的。您可以根据您的输入源类似地使用FormParamQueryParam

关于java - 如何使用 Dropwizard 解析 RESTful API 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43057502/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com