java - 使用 Spring Boot Java 的补丁请求

标签 java spring-boot openshift http-patch

我的 OpenShift Api 补丁请求有问题。 这是我的实际代码:


public static  HttpStatus PatchHTTPRequestCustomHeaders(String url, String data) {

        String Bearer = "ayJhbGciOiJSUzI1NiIs...";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);     
        headers.set("Authorization", "Bearer "+Bearer);
        headers.set("X-HTTP-Method-Override",  "PATCH");
        headers.set("Content-Type", "application/json-patch+json");
        headers.set("Accept", "application/json");
        final HttpEntity<String> entity = new HttpEntity<String>(data, headers);
        RestTemplate restTemplate = new RestTemplate();

            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class, 1);
            if(response.getStatusCode() == HttpStatus.OK) {
                return HttpStatus.OK;
            }else {
                return null;
            }  
    }

数据包含操作替换或添加或删除...

PostMan 错误:500 内部服务器错误

Spring :

2020-02-14 10:12:28.918 ERROR 14944 --- [nio-8169-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException$MethodNotAllowed: 405 Method Not Allowed: [{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"the server does not allow this method on the requested resource","reason":"MethodNotAllowed","details":{},"code":405}
]] with root cause

org.springframework.web.client.HttpClientErrorException$MethodNotAllowed: 405 Method Not Allowed: [{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"the server does not allow this method on the requested resource","reason":"MethodNotAllowed","details":{},"code":405}
]

我的 Php 补丁没有任何问题...

我希望有人知道发生这种情况的原因。

编辑:

public static void disableSSLCertificateChecking() {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                // Not implemented
            }

            @Override
            public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                // Not implemented
            }
        } };

        try {
            SSLContext sc = SSLContext.getInstance("TLS");

            sc.init(null, trustAllCerts, new java.security.SecureRandom());

            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

最佳答案

发送补丁请求时我遇到了同样的问题。 这是解决方案:

restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory())

关于java - 使用 Spring Boot Java 的补丁请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60223264/

相关文章:

java - 严重 : Begin event threw error java. lang.ExceptionInInitializerError Intellij/Tomcat

spring - 使用Spring Boot在嵌入式Tomcat服务器上部署资源

apache-spark - Airflow -如何使用不退出的命令运行KubernetesPodOperator

kubernetes - oc get pods - 命令只打印 pod 名称

java - java程序运行60M或90M正常吗?

java - 找不到以下类: com. codecarpet.fbconnect.FBLoginButton

java - 比较 2 个文本文件并找出列表中的差异并找出哪些列表值不匹配

sql - 带有 nativeQuery 的 Spring Boot Query 注释在 Postgresql 中不起作用

java - 在spring boot中使用openapi使用@Schema(allowableValues=)作为枚举参数

openshift - 如何在 Openshift 中调试卡带脚本?