简体   繁体   English

使用REST删除http

[英]http delete with REST

I am currently using Jersey Framework (JAX-RS implementation) for building RESTful Web Services. 我目前正在使用Jersey Framework(JAX-RS实现)来构建RESTful Web服务。 The Resource classes in the project have implemented the standard HTTP operations - GET,POST & DELETE. 项目中的Resource类已经实现了标准的HTTP操作--GET,POST和DELETE。 I am trying to figure out how to send request parameters from client to these methods. 我试图弄清楚如何从客户端向这些方法发送请求参数。

For GET it would be in the query string(extract using @QueryParam ) and POST would be name/value pair list (extract using @FormParam ) sent in with the request body. 对于GET,它将在查询字符串中(使用@QueryParam提取),POST将是与请求正文一起发送的名称/值对列表(使用@FormParam提取)。 I tested them using HTTPClient and worked fine. 我使用HTTPClient测试它们并且工作正常。 For DELETE operation, I am not finding any conclusive answers on the parameter type/format. 对于DELETE操作,我没有找到关于参数类型/格式的任何结论性答案。 Does DELETE operation receive parameters in the query string(extract using @QueryParam ) or in the body(extract using @FormParam )? 难道DELETE操作接收参数的查询字符串(使用提取@QueryParam )或在体内(使用提取@FormParam )?

In most DELETE examples on the web, I observe the use of @PathParam annotation for parameter extraction(this would be from the query string again). 在Web上的大多数DELETE示例中,我观察到使用@PathParam注释进行参数提取(这将再次来自查询字符串)。

Is this the correct way of passing parameters to the DELETE method? 这是将参数传递给DELETE方法的正确方法吗? I just want to be careful here so that I am not violating any REST principles. 我只是想在这里小心,以便我不违反任何REST原则。

Yes, its up to you, but as I get REST ideology, DELETE URL should delete something that is returned by a GET URL request. 是的,由您决定,但是当我获得REST意识形态时,DELETE URL应该删除由GET URL请求返回的内容。 For example, if 例如,如果

GET http://server/app/item/45678

returns item with id 45678, 返回ID为45678的项目,

DELETE http://server/app/item/45678

should delete it. 应该删除它。

Thus, I think it is better to use PathParam than QueryParam, when QueryParam can be used to control some aspects of work. 因此,我认为使用PathParam比使用QueryParam更好,因为QueryParam可用于控制工作的某些方面。

DELETE http://server/app/item/45678?wipeData=true

The DELETE method should use the URL to identify the resource to delete. DELETE方法应使用URL来标识要删除的资源。 This means you can use either path parameters or query parameters. 这意味着您可以使用路径参数或查询参数。 Beyond that, there is no right and wrong way to construct an URL as far as REST is concerned. 除此之外,就REST而言,构建URL没有正确和错误的方法。

You can use like this 你可以像这样使用

URL is http://yourapp/person/personid URL为http:// yourapp / person / personid

@DELETE
@Path("/person/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response deletePerson(@PathParam("id") String id){
    Result result = new Result();
    try{
        persenService.deletePerson(id);
        result.setResponce("success"); 
    }
    catch (Exception e){
        result.setResponce("fail");
        e.printStackTrace();
    }
    return Response.status(200).entity(result).build();
}

@QueryParam would be the correct way. @QueryParam将是正确的方法。 @PathParam is only for things before any url parameters (stuff after the '?'). @PathParam仅用于任何url参数之前的东西('?'之后的东西)。 And @FormParam is only for submitted web forms that have the form content type. @FormParam仅适用于具有表单内容类型的已提交Web表单。

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

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