【问题标题】:Spring Boot RESTful Web Service to post json content to https(secure) urlSpring Boot RESTful Web 服务将 json 内容发布到 https(secure) url
【发布时间】:2018-07-11 22:01:36
【问题描述】:

我正在寻找 spring boot 的工作代码示例/sn-p,以将 json 内容发布到 HTTPS restful web 服务(用 python 开发)。下面是执行相同操作的独立程序的示例代码,但我想在 Spring Boot Restful 应用程序中执行此操作。我在 google 和堆栈溢出中找到了许多示例 example example 但这些是用于获取请求,而不是用于我正在寻找的套件。有人请分享“https post request using spring boot service”的完整工作示例。 提前致谢。

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

【问题讨论】:

    标签: java spring-boot ssl-certificate


    【解决方案1】:

    好的,这里是forked Github repo

    以下是我所做的更改:

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

    secure-client -> 添加了对该 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();
        }
    }
    

    所以,当您调用客户端端点时:

    http://localhost:8086/post
    

    你会得到回应:

    Hello codependent-client! You sent: Test payload
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2013-04-18
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多