简体   繁体   English

使用 Spring Boot Java 的补丁请求

[英]Patch Request using Spring Boot Java

I have a problem with my Patch Request with OpenShift Api.我的 OpenShift Api 补丁请求有问题。 This is my actual code :这是我的实际代码:


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

The data contains a op replace or add or remove ...数据包含op replace or add or remove ...

Error on PostMan : 500 Internal Server Error PostMan 上的错误:500 内部服务器错误

On 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}
]

I don't have any problem with my Patch in Php...我在 PHP 中的补丁没有任何问题...

I hope someone know the reason of what is happening.我希望有人知道发生的事情的原因。

EDIT :编辑 :

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

I got same problem when sending a Patch request.发送补丁请求时我遇到了同样的问题。 Here is the solution for this:这是解决方案:

restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory())

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

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