【问题标题】:Patch Request using Spring Boot Java使用 Spring Boot Java 的补丁请求
【发布时间】:2020-02-14 09:15:27
【问题描述】:

我的 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;
            }  
    }

数据包含op replace or add or remove ...

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

春天:

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();
        }
    }

【问题讨论】:

    标签: java spring-boot openshift http-patch


    【解决方案1】:

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

    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory())
    

    【讨论】:

    • 谢谢,它似乎有帮助,但我使用我自己的方法disableSSLCertificateChecking() 基于TrustManager,以便不检查证书。在我发出您的命令之前,此方法运行良好。现在证书错误再次出现。
    • 我刚刚将证书添加到java中,原来的错误又回来了..(我添加了你的代码..)
    • 你的回复如果是答案的一部分,所以我验证它,(我需要从HttpMethod.POST修改为HttpMethod.PATCH
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 2013-11-16
    • 1970-01-01
    • 2022-12-22
    • 1970-01-01
    • 2016-02-12
    相关资源
    最近更新 更多