Java Spring Boot RESTful Web服务将json内容发布到https(安全)url

Java Spring Boot RESTful Web服务将json内容发布到https(安全)url,java,spring-boot,ssl-certificate,Java,Spring Boot,Ssl Certificate,我正在搜索SpringBoot的工作代码示例/片段,以将json内容发布到HTTPS restful web服务(用python开发)。下面是独立程序的示例代码,它也有同样的功能,但我想在SpringBootRESTful应用程序中实现。我在google和stack overflows中找到了很多例子,但这些都是get请求的例子,而不是我正在寻找的套件。有人请分享“使用spring引导服务的https post请求”的完整工作示例“。 提前谢谢 import java.io.PrintStrea

我正在搜索SpringBoot的工作代码示例/片段,以将json内容发布到HTTPS restful web服务(用python开发)。下面是独立程序的示例代码,它也有同样的功能,但我想在SpringBootRESTful应用程序中实现。我在google和stack overflows中找到了很多例子,但这些都是get请求的例子,而不是我正在寻找的套件。有人请分享“使用spring引导服务的https post请求”的完整工作示例“。 提前谢谢

import java.io.PrintStream;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HttpsURLConnection;

public class App{

     public static void main(String []args) throws NoSuchAlgorithmException, KeyManagementException{

        String https_url = "https://192.168.4.5:55543/books";
        String content = "{\"data\":{\"a\"}}";
        System.setProperty("javax.net.useSystemProxies", "true");

        System.setProperty("javax.net.ssl.keyStore", "C:/cert/client.jks");
        System.setProperty("javax.net.ssl.keyStorePassword", "testdev");

        System.setProperty("javax.net.ssl.trustStore", "C:/cert/server.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "testdev");

        System.setProperty("jdk.tls.client.protocals", "TLSv1.2");
        System.setProperty("https.protocals", "TLSv1.2");

        HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> { return true;});
        URL url = new URL(https_url);
        HttpsURLConnection https_con = (HttpsURLConnection) url.openConnection();

        https_con.setRequestProperty("Content-Type", "application/json");
        https_con.setRequestMethod("POST");
        https_con.setDoOutput(true);
        PrintStream ps = new PrintStream(https_con.getOutputStream());
        ps.println(content);
        ps.close();
        https_con.connect();
        https_con.getResponseCode();
        https_con.disconnect();
     }
}
好的,这是答案

以下是我所做的更改:

secure server
->添加了带有简单字符串负载的post端点:

@RestController
public class HomeRestController {
    //...
    @PostMapping(value = "/", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String consumeData(@RequestBody String data, Principal principal){
        return String.format("Hello %s! You sent: %s", principal.getName(), data);
    }
}
安全客户端
->添加了对该post方法的调用:

@RestController
public class HomeRestController {
    // . . .

    @GetMapping("/post")
    public String post() throws RestClientException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String data = "Test payload";

        HttpEntity<String> request = new HttpEntity<>(data, headers);

        return restTemplate.postForEntity("https://localhost:8443", request , String.class ).getBody();
    }
}
您将得到以下答复:

Hello codependent-client! You sent: Test payload

你的例子不是在模仿我的例子,你能通过重构你的代码来分享准确的代码吗?我正在寻找类似的相互认证作为post请求的认证例子。Thanksher是类似的mitual auth get请求示例[我正在寻找“post”requestOk,但是没有在spring boot Rest客户端中加载密钥库和信任库证书有什么具体原因吗?我没有看到代码库这是一个示例,证书加载SSLContextBuilder()之间有什么区别.loadTrustMaterial()与System.setProperty()方法?谢谢
Hello codependent-client! You sent: Test payload