【问题标题】:@QueryParam boolean gets false even though 'true' was passed即使传递了“真”,@QueryParam 布尔值也会变为假
【发布时间】:2015-10-21 11:19:32
【问题描述】:

使用布尔参数调用 REST 会收到值 false,即使在客户端传递了 true

客户:

$http.post("http://localhost/getServers/?light=true")

服务器:

@Path("/getServers")
@POST
@Produces({MediaType.APPLICATION_JSON})
public Response getServers(
  @Context HttpServletRequest request,
  @DefaultValue("true") @QueryParam("light") boolean light)
{
  // light is false even though true was passed
  ...
}

【问题讨论】:

    标签: java rest http javax.ws.rs queryparam


    【解决方案1】:

    似乎问号 (?) 之前的斜线 (/) 是问题所在。

    删除客户端的斜线后,一切正常。

    这行得通:

    $http.post("http://localhost/getServers?light=true")
    

    但是,从网上阅读来看,问号前的斜杠是合法的语法:(

    【讨论】:

    • 在一般的 HTTP 请求中是合法的,但对你写的合约不合法,即@Path("/getServers")@Path("/getServers/") 不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多