简体   繁体   English

对 Java Spring API 的 PATCH 请求

[英]PATCH Request into Java Spring API

i would like to make a PATCH request in the OpenShift API.我想在 OpenShift API 中发出 PATCH 请求。 To make it easier, we consider that we see this in the OpenShift API :为方便起见,我们认为我们在 OpenShift API 中看到了这一点:

The content can be this :内容可以是这样的:

{ 
  "limits":{ 
    "cpu":"10m"
  },
  "requests":{ 
    "memory":"1G"
  }
}

Or this :或这个 :

{ 
  "limits":{ 
    "cpu":"30m",
    "memory":"2G"
  },
  "requests":{ 
    "memory":"5G"
  }
}

Or ..., You see what i mean ;) Keys from the Json can be or cannot be.或者...,你明白我的意思;) 来自 Json 的密钥可以是也可以不是。

So now, i would like to know how to make a Patch without having to know what was in in the past, because the code will only the receive new values, empty or not.所以现在,我想知道如何制作补丁而不必知道过去是什么,因为代码只会接收新值,无论是否为空。

I hope to be clear :) I use JDK 13 and Spring with Maven我希望清楚:) 我将 JDK 13 和 Spring 与 Maven 一起使用

Thanks谢谢

The answer : Get the old data by making a get method to the API which contains the old date.答案:通过对包含旧日期的 API 进行 get 方法来获取旧数据。 And then, i will be able to know changes for patching !然后,我将能够知道修补的变化! :) :)

The most appropriate solution seems to be making your requests as json-patch requests.最合适的解决方案似乎是将您的请求作为json-patch请求。 Formatting Docs: http://jsonpatch.com/格式化文档: http : //jsonpatch.com/

In my example, I am updating the "image" property of my DeploymentConfig, but this should work for other kinds of objects.在我的示例中,我正在更新 DeploymentConfig 的“image”属性,但这应该适用于其他类型的对象。 Note that you can make more than one patch command per request, and each patch command only needs to know about the properties making up the path you are altering.请注意,您可以为每个请求创建多个补丁命令,并且每个补丁命令只需要了解构成您正在更改的路径的属性。

My request setup: configurations docs我的请求设置: 配置文档

  • Request: PATCH /apis/apps.openshift.io/v1/namespaces/$NAMESPACE/deploymentconfigs/$NAME请求:PATCH /apis/apps.openshift.io/v1/namespaces/$NAMESPACE/deploymentconfigs/$NAME
  • Auth: Bearer $TOKEN身份验证:不记名 $TOKEN
  • Headers:标题:

    • Content-Type: application/json-patch+json内容类型:application/json-patch+json
    • Accept: application/json接受:应用程序/json
    • Connection: close连接:关闭
  • Body:身体:

[
    {
        "op": "replace", 
        "path": "/spec/template/spec/containers/0/image", 
        "value": "my-repo-image.url/my-image:v10"
    },
    { "op": "add", ... },
    { "op": "remove", ...},
    ...
]

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

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