简体   繁体   English

Jersey HTTP Delete,Put 响应状态:405(方法不允许)

[英]Jersey HTTP Delete,Put Response Status: 405 (Method Not Allowed)

Day 1: Added below rest endpoint for delete operation.第 1 天:在 rest 端点下方添加了删除操作。

@Path("/company/v1/department")
@Component
public class ManageResource {

@DELETE
@Path("/{identifier}/{identifier_value}/employee")
public void delete(@PathParam("identifier") String identifier,
                   @PathParam("identifier_value") final String identifierValue,
                   @QueryParam("age") final String age) {

 //delete operation
}
}

I was able to invoke DELETE endpoint using postman with below request:我能够使用具有以下请求的邮递员调用 DELETE 端点:

DELETE: http://localhost:8080/company/v1/department/name/baner/employee?age=50

Day 2: Added below rest endpoint for the update operation in the same resource.第 2 天:在同一资源中的更新操作的下面添加了 rest 端点。

@Path("/company/v1/department")
@Component
public class ManageResource {

@DELETE
@Path("/{identifier}/{identifier_value}/employee")
public void delete(@PathParam("identifier") String identifier,
                   @PathParam("identifier_value") final String identifierValue,
                   @QueryParam("age") final String age) {
   
//delete operation
}

@PUT
@Path("/empid/{value}/employee")
@Consumes(MediaType.APPLICATION_JSON)
public void update(@PathParam("value") final String identifierValue,
                   @RequestBody final EmployeeUpdateRequest request) {
   
//update operation
}
}

After adding this new endpoint, I am able to invoke PUT using postman with below request:添加此新端点后,我可以使用邮递员通过以下请求调用 PUT:

PUT: http://localhost:8080/company/v1/department/empid/epid-123/employee
{
//Json request body
}

But when I try to invoke Delete endpoint it is giving me 405 (Method Not Allowed) error.但是当我尝试调用 Delete 端点时,它给了我 405 (Method Not Allowed) 错误。 If I comment my new Put method, then the Delete method works fine.如果我评论我的新 Put 方法,则 Delete 方法工作正常。 Also, if I replace Path for Put method to "/{identifier}/{identifier_value}/employee" then both Delete and Put method works fine.另外,如果我将 Put 方法的 Path 替换为“/{identifier}/{identifier_value}/employee”,那么 Delete 和 Put 方法都可以正常工作。

I am using Jersey 1.19 with tomcat.我在 tomcat 中使用 Jersey 1.19。 Can someone help me with this?有人可以帮我弄这个吗?

Your Paths are in conflict with each other.你的路径相互冲突。 Let me try to explain:让我试着解释一下:

DELETE = /{identifier}/{identifier_value}/employee 
PUT = /empid/{value}/employee

That means when we evaluate the path from left to right, we can either have {identifier} which is anything or "empid" which is a fixed string这意味着当我们评估从左到右的路径时,我们可以有 {identifier} 是任何东西,也可以是固定字符串的“empid”

Jersey always tries to find the "most perfect" match for a REST endpoint. Jersey 总是试图为 REST 端点找到“最完美”的匹配。 It does so by evaluating the path from left to right.它通过评估从左到右的路径来实现。 Fixed strings always take precedence before random variables!固定字符串总是优先于随机变量!

Basically that means when you want to call a DELETE, you cannot have the value "empid" for the variable "{identifier}" because then you are already out-of-scope基本上这意味着当你想调用一个 DELETE 时,你不能有变量“{identifier}”的值“empid”,因为你已经超出了范围

So the DELETE call to所以 DELETE 调用

http://localhost:8080/company/v1/department/empid/empid-123/employee

will not work as Jersey had to make a decision whether "empid" in the request matches "{identifier}" (DELETE) or "empid" (PUT).将不起作用,因为 Jersey 必须决定请求中的“empid”是匹配“{identifier}”(DELETE)还是“empid”(PUT)。 And as i tried to explain above, fixed strings take a higher priority.正如我在上面试图解释的那样,固定字符串具有更高的优先级。 In contrast, any other DELETE request where相反,任何其他 DELETE 请求,其中

http://localhost:8080/company/v1/department/{identifier}/empid-123/employee

and

{identifier} != "empid"

works.作品。

Possible solution:可能的解决方案:

make your rest endpoints resource-oriented使您的休息端点面向资源

DELETE: /employee/{employee-id}删除:/employee/{employee-id}

PUT: /employee/{employee-id}放置:/employee/{employee-id}

Notice how the endpoints are identical, since other than the ID in most systems, no information is needed to identify an entity.注意端点是如何相同的,因为除了大多数系统中的 ID 之外,不需要任何信息来识别实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM