【问题标题】:Jersey client bad request泽西客户错误请求
【发布时间】:2015-10-04 15:11:41
【问题描述】:

我有以下客户端代码:

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();
}

当我在文件路径下发送时,此客户端抛出错误请求异常:

String filePath = "/testzip/123/TEST-test.zip";

但是当我发送到下面的文件路径(简单字符串)时它工作正常:

String filePath = "testzip";

当路径参数中存在正斜杠(/)时,我无法弄清楚为什么它不起作用。

【问题讨论】:

  • 试着做target.path(filePath).reqest()。为什么MediaType.APPLICATION_JSON?对请求资源没有多大意义。
  • @peeskillet 我删除了MediaType.APPLICATION_JSON,因为它没有意义。 target.path(filePath).reqest() 对我不起作用。它说“请求的资源不可用。”因为它将斜杠作为一些资源路径并试图在服务器端找出该资源

标签: java rest jersey jax-rs


【解决方案1】:

我相信默认情况下,@PathParam 中不能包含 /

编辑看看这里:Tomcat, JAX-RS, Jersey, @PathParam: how to pass dots and slashes?

【讨论】:

    猜你喜欢
    • 2016-10-23
    • 2013-08-02
    • 2014-05-22
    • 2011-08-04
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多