简体   繁体   English

将HTTP DELETE请求实体主体映射到方法参数

[英]Mapping an HTTP DELETE request entity body to a method parameter

I am building a REST API using JAX-RS . 我正在使用JAX-RS构建REST API In angular front-end, I am sending the object to be deleted in the body of the HTTP request (JSON format). 在有角前端中,我正在HTTP请求(JSON格式)的主体中发送要删除的对象。 Now I need a way to map this HTTP DELETE body request which is containing the object that needs to be deleted to a local variable in the REST method. 现在,我需要一种映射此HTTP DELETE主体请求的方法,该请求包含需要删除的对象到REST方法中的局部变量。 For instance, on SPRING I did this by simply annotating an object variable with @RequestBody . 例如,在SPRING我通过使用@RequestBody注释对象变量来做到这一点。
I was checking oracle's javaEE7 docs but the examples there are really basic and don't include complex objects, also the different tutorials that I found elsewhere were on the track of simple delete requests mapping a simple id with @PathParam . 我正在查看oracle的javaEE7文档,但是其中的示例确实很基本,并且不包含复杂的对象,我在其他地方找到的不同教程也都在简单删除请求的轨迹上,这些请求使用@PathParam映射了一个简单的id。

Maybe before this question, the first question I should ask is whether sending the object in an HTTP's request body is at all a good approach? 也许在这个问题之前,我要问的第一个问题是在HTTP的请求正文中发送对象是否完全是一种好方法? I was reading some articles which designated it as not such a good practice, although it is not explicitly forbidden. 尽管没有明确禁止,但我正在阅读一些文章,将其指定为不好的做法。 What would be the disadvantages of this approach? 这种方法的缺点是什么? I remember while I was researching about this method in SPRING , I read somewhere that malicious attacks could be possible by specially crafted user inputs (the persistence framework that I am using is JPA, EclipseLink). 我记得在SPRING研究此方法时,我读到某个地方,通过特制的用户输入可能会进行恶意攻击(我使用的持久性框架是JPA,EclipseLink)。 Would it perhaps be better to map the primary key on a series of @Path variables and then map them using @PathParam ? 将主键映射到一系列@Path变量,然后使用@PathParam映射它们会@PathParam吗?

So to sum up, first of all, is this a good approach? 因此,总而言之,这是一个好方法吗? And how can I read the object in the HTTP's request body? 以及如何读取HTTP请求正文中的对象? Some pointers would be highly appreciated! 一些指针将不胜感激!

Unlike Spring MVC, JAX-RS does not define any annotation for the request payload. 与Spring MVC不同,JAX-RS不会为请求有效负载定义任何注释。

The JAX-RS approach is slightly different: the value of the parameter not annotated with any @***Param annotations is mapped from the request entity body. JAX-RS方法略有不同:未使用任何@***Param批注进行批注的参数值是从请求实体主体映射的。 Such parameter is called entity parameter . 这样的参数称为实体参数


The first question I should ask is whether sending the object in an HTTP's request body is at all a good approach? 我应该问的第一个问题是,在HTTP的请求正文中发送对象是否完全是一种好方法?

Please refrain from doing that, as it's not how DELETE is supposed to work. 请不要这样做,因为这不应该是DELETE工作方式。

Find below a quote from the RFC 7231 , the document that currently defines the semantics and content of the HTTP/1.1 protocol: RFC 7231的引号下方找到该文档,该文档当前定义了HTTP / 1.1协议的语义和内容:

A payload within a DELETE request message has no defined semantics; DELETE请求消息中的有效负载没有定义的语义。 sending a payload body on a DELETE request might cause some existing implementations to reject the request. DELETE请求上发送有效内容主体可能会导致某些现有实现拒绝该请求。

For interoperability, I advise you to stick to the standards as much as you can. 为了实现互操作性,我建议您尽可能遵循标准。 And you definitely shouldn't be require to send any payload to identify the resource to be deleted. 而且,您绝对不应该要求发送任何有效负载来标识要删除的资源。


Its primary key is 8 fields long. 它的主键长8个字段。

The URI , which stands for U niversal R esource I dentifier, is meant to identify a resource . URI,它代表üniversal 方案资源 dentifier,是为了识别资源

As you have a bunch of fields that, in conjunction, identify a resource, I advise you to rethink your application design. 由于您有一堆字段可以共同标识资源,因此建议您重新考虑应用程序设计。 You could, for example, introduce some sort of unique value to identify your resources. 例如,您可以引入某种独特的价值来识别您的资源。 Have a look at UUID . 看看UUID

With JAX-RS you don't need a something like a @RequestBody. 使用JAX-RS,您不需要@RequestBody之类的东西。

You can simply add the class as parameter and it will be filled with the request body. 您可以简单地将类添加为参数,并将其填充为请求正文。

In you case passing the data in the body makes sense but how does your URL look like? 在您看来,将数据传递到正文中是有道理的,但是您的URL看起来如何? As with REST you should have resources that are addressable by a URL 与REST一样,您应该拥有可通过URL寻址的资源

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

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