Jersey:解码字符串@PathParam,其中包含 space

Jersey: Decoding String @PathParam which contains space in it

public Response getCustomerByName(
@PathParam("customerName") String customerName)

问题
我将 customerName 传递为:stack overflow(URL 编码为:stack%20overflow)。我想在我的 java 代码中接收解码后的字符串(堆栈溢出,没有 %20)。

我试过的
这工作得很好,但我觉得这不是更通用的方法。

URLDecoder.decode(customerName, "UTF-8");

需要更通用的解决方案: 我也想对 API 的其余部分进行类似的更改,因此在每个 API 中使用 URL 解码器是负担。有没有我可以遵循的在应用程序级别强加这种解码的常见做法? (@PathParam 收到请求时已经解码)

它应该是自动的 "Decoded" 并且您不需要使用 URLDecoder.decode(customerName, "UTF-8");

显式解码

PathParam javadoc 的 javadoc 中所述:

The value is URL decoded unless this is disabled using the Encoded annotation.

我刚刚在下面进行了验证,它按照 javadoc(在 weblogic 服务器中)工作

@GET
@Produces(value = { "text/plain"})
@Path("{customerName}")
public Response getCustomerByName(@PathParam("customerName") String customerName) {
    System.out.println(customerName);
    return Response.ok().entity(customerName).type("text/plain").build();
}