泽西岛客户端错误请求
Jersey client bad request
我有以下客户端代码:
String filePath = "/testzip/123/TEST-test.zip";
target = mainTarget.path("file").path("{filePath}");
Invocation.Builder invocationBuilder = target
.resolveTemplate("filePath", filePath)
.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_OCTET_STREAM);
Response response = invocationBuilder.get();
下面是我的服务器代码:
@GET
@Path("{filePath}")
@Produces({MediaType.APPLICATION_OCTET_STREAM})
public Response get(@PathParam("filePath") String filePath) {
File file = new File(filePath);
return Response.status(Response.Status.OK).entity(file).build();
}
当我发送以下文件路径时,此客户端抛出 Bad Request 异常:
String filePath = "/testzip/123/TEST-test.zip";
但是当我在文件路径(简单字符串)下方发送时它工作正常:
String filePath = "testzip";
我无法弄清楚为什么当路径参数中存在正斜杠 (/) 时它不起作用。
我相信默认情况下 @PathParam
中不能有 /
。
编辑 看看这里:Tomcat, JAX-RS, Jersey, @PathParam: how to pass dots and slashes?
我有以下客户端代码:
String filePath = "/testzip/123/TEST-test.zip";
target = mainTarget.path("file").path("{filePath}");
Invocation.Builder invocationBuilder = target
.resolveTemplate("filePath", filePath)
.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_OCTET_STREAM);
Response response = invocationBuilder.get();
下面是我的服务器代码:
@GET
@Path("{filePath}")
@Produces({MediaType.APPLICATION_OCTET_STREAM})
public Response get(@PathParam("filePath") String filePath) {
File file = new File(filePath);
return Response.status(Response.Status.OK).entity(file).build();
}
当我发送以下文件路径时,此客户端抛出 Bad Request 异常:
String filePath = "/testzip/123/TEST-test.zip";
但是当我在文件路径(简单字符串)下方发送时它工作正常:
String filePath = "testzip";
我无法弄清楚为什么当路径参数中存在正斜杠 (/) 时它不起作用。
我相信默认情况下 @PathParam
中不能有 /
。
编辑 看看这里:Tomcat, JAX-RS, Jersey, @PathParam: how to pass dots and slashes?