Jersey在方法类型之前匹配正则表达式模式

时间:2013-08-07 13:10:32

标签: regex jersey jax-rs

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

@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请求。

1 个答案:

答案 0 :(得分:0)

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

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

这应该有效。然后,您需要确保在CURL请求中提供正确的数据,以确保在将其重新放入请求时它与@Consumes注释匹配。