如何在 RESTEasy 中实现补丁请求? [英] How to implement patch requests in RESTEasy?

查看:65
本文介绍了如何在 RESTEasy 中实现补丁请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个 Patch 请求(json 格式)中实现多个操作.RESTEasy 不支持开箱即用的补丁请求.如何提供自定义实现?

I would like to implement multiple operations in one Patch request (json format). RESTEasy doesn't support Patch requests out-of-box. How to provide custom implementation?

推荐答案

要启用 PATCH,您需要定义一个注释为 @HttpMethod:

To enable PATCH you need to define a annotation annotated with @HttpMethod:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("PATCH")
public @interface PATCH {}

Bill Burke 的RESTful Java with JAX-RS 2.0"一书中描述了一个完整的示例.源代码可以在 resteasy 存储库中找到.

A full example is described in Bill Burke's book "RESTful Java with JAX-RS 2.0". The sourcecode can be found in the resteasy repository.

也许是 JAX-RS 2.1.将 支持 PATCH 开箱即用.

Maybe JAX-RS 2.1. will support PATCH out of the box.

更新:如果您想在一个请求中修补多个资源,您需要先识别它们.例如,如果您想为所有具有一定营业额的客户提供 VIP 身份,您可以使用如下资源方法:

Update: If you want to patch multiple resources in one request you need to identify them first. For instance if you want to give all customers with a certain turnover the VIP status you could have a resource-method like this:

@PATCH
@Path("/customers")
public Response patchCustomers(@QueryParam("minTurnover") Double minTurnover, InputStream is) {
   // find and update customers
}

实体主体中传递哪些信息由您决定.RFC 要求应应用于资源的一组更改".这可以是简单的text/plain,比如update: vip=true.此类更新的标准格式是 json-patch:

Which information is passed in the entity body is up to you. The RFC demands "a set of changes" which should be applied to the resource. This could be simple text/plain like update: vip=true. A standard-format for such updates is json-patch:

PATCH /customers?minTurnover=1000 HTTP/1.1
Content-Type: application/json-patch

[
  { 
    "op" : "replace", 
    "path" : "/vip", 
    "value" : "true" 
  },
  {
    ... more operations ...
  }
]

请注意,应将同一组操作应用于所有已识别的资源.

Note that the same set of operations should be applied to all identified resources.

这篇关于如何在 RESTEasy 中实现补丁请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆