Java中的补丁方法

Patch method in Java

嗯,我在 Postman 中有这个请求:

127.0.0.1:8000/empleado/615050/retrieve-update/

然后我添加这个正文:

{"nombre":Marc}

这个请求对我有用,在我的 API Django 开发中

但是,当我尝试在我的 Java 桌面应用程序中复制此请求时,失败并且没有任何反应。这是我的 Java 代码:

public void sendJson() {

        JsonObjectBuilder o = Json.createObjectBuilder();
        o.add("contrasenia", txtPass.getText());
        JsonObject contra = o.build();
        String contraS = contra.toString();
        System.out.println(contraS);
        
        try {
            URL url = new URL("http://localhost:8000/empleado/" + txtUsuario.getText() + "/retrieve-update/");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("PATCH");
            conn.setRequestProperty("Content-Type","application/json;charset=UTF-8");
            conn.setDoOutput(true);
            conn.setDoInput(true);

            OutputStream os = conn.getOutputStream();
            os.write(contraS.getBytes(StandardCharsets.UTF_8));
            os.close();
   

        } catch (MalformedURLException malformedURLException) {
            malformedURLException.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

想法?谢谢,对不起我的英语

这是因为,不幸的是,PATCH 不是 HttpURLConnection

的合法动词

请参阅 Javadoc HttpURLConnection#setRequestMethod

Set the method for the URL request, one of:
GET
POST
HEAD
OPTIONS
PUT
DELETE
TRACE
are legal, subject to protocol restrictions. The default method is GET.

here 等解决方法,但您的服务器应与自定义 header.

兼容

如果我是你,我可能会尝试使用更高级的 HTTP 客户端

或任何其他