0

Jersey是否可以使用相同的正则表达式但不同类型定义 2 个方法?( GET, PUT..):

@GET
@Path("{key: .+}")
@Produces(MediaType.TEXT_PLAIN)
public Response root(String key) {
}

@PUT
@Path("{key: .+}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response publish(String key, FormDataMultiPart data) {
}

第一种方法应该只回复密钥(带或不带斜杠)

curl -X GET "http://localhost/key
Jersey respond with 200 OK since it went to the GET method

curl -X GET "http://localhost/key/
Jersey respond with 200 OK since it went to the GET method

curl -X PUT -T file.txt "http://localhost/key
Jersey respond with 200 OK since it went to the PUT method

curl -X PUT -T file.txt "http://localhost/key/
Jersey respond with 200 OK since it went to the PUT method

curl -X PUT -T file.txt "http://localhost/key/folder/folder
Jersey respond with 405 Method Not Found since it went to the GET method
instead of the PUT (the get only respond to 1 folder level which is the 'key'
but i expected that jersey will go directly to the PUT since it suppose to check for the method type before the regex matching

为什么最后一个不起作用?似乎泽西岛首先寻找正则表达式,即使它是一个PUT请求。

4

1 回答 1

0

您说“泽西以 405 方法未找到响应,因为它使用了 GET 方法而不是 PUT”,但 405 意味着它没有使用任何方法。尝试将您的 PUT 方法更改为:

@PUT
@Path("{key: .+}")
@Produces(MediaType.TEXT_PLAIN)
public Response publish(String key) {
}

这应该有效。然后,您需要确保提供正确的数据作为 CURL 请求的一部分,以确保在将@Consumes注释放回请求时它与注释匹配。

于 2013-08-07T17:59:34.917 回答