gpt4 book ai didi

spring - 使用不同的@QueryParams 重载路径函数

转载 作者:IT老高 更新时间:2023-10-28 13:57:50 25 4
gpt4 key购买 nike

我想为同一个 GET 路径设置多个函数。

当且仅当查询参数与 URL 字符串中的参数匹配时,我希望我的 Web 服务“找到”这些函数。

例如:

我有 Path("/myGET")

对于那条路径,我希望有 2 个功能:

@GET  
@Produces(MediaType.APPLICATION_JSON)
@TypeHint(TagSets.class)
public Response getTagSets(@QueryParam("entityId") Integer entityId)
{
...
}

还有

@GET  
@Produces(MediaType.APPLICATION_JSON)
@TypeHint(TagSets.class)
public Response getTagSets(){
...
}

现在我收到一个错误:

SEVERE: The following errors and warnings have been detected with resource and/or provider classes: SEVERE: Producing media type conflict. The resource methods public javax.ws.rs.core.Response<...>.getTagSets(java.lang.Integer) and public javax.ws.rs.core.Response<...>.getTagSets(java.lang.Integer,java.lang.Integer) can produce the same media type SEVERE: Producing media type conflict. The resource methods public javax.ws.rs.core.Response<...>.getTagSets() and public javax.ws.rs.core.Response <...>.getTagSets(java.lang.Integer,java.lang.Integer) can produce the same media type

首先:有什么办法可以实现我想要在这里做的事情..

秒:如果这是可用的,当且仅当查询参数与函数中请求的内容完全匹配时,是否有任何方法可以找到路径?例如,如果将使用 @QueryParam("differentParam") 调用相同的路径,它将不会到达 2 个函数中的任何一个。

第三:如果 Jersey 无法做到这一点,有没有其他框架可以做到这一点?

重要提示:回答问题的人认为我正在寻找解决方法而不是解决方案。今天我正在使用 1 个函数并检查参数并从中调用我需要的东西(这是我在发布问题之前使用的)。但我正在寻找的可能是使用框架功能来为我省去麻烦

谢谢。

最佳答案

资源由其路径唯一定义,而不是由其参数定义。您定义的两个资源具有相同的路径。您可以为它们中的每一个定义新路径,例如 /myGet/entity/myGet//myGet/differentParam;或使用单个路径作为 /myGet/ 并检查查询参数如下:

@GET  
@Produces(MediaType.APPLICATION_JSON)
@TypeHint(TagSets.class)
public Response getTagSets(@Context HttpServletRequest request){

...

if (request.getParameterMap().isEmpty()) {
// then you have no query params, implement as there are no query params
} else {
String queryParam = request.getQueryString();
// check queryParam, and add another if else statements, implement
}

...

}

关于spring - 使用不同的@QueryParams 重载路径函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18465101/

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