From 99fefabff32c9bd95c37b363cf17508063c6ed48 Mon Sep 17 00:00:00 2001 From: wangyu Date: Mon, 29 Jul 2024 18:58:22 +0800 Subject: [PATCH] feature : init project --- pom.xml | 12 +- .../szyx/tcm/supervision/config/HttpConfig.java | 17 ++- .../controller/insurance/ProxyController.java | 14 +- src/main/resources/application.properties | 4 +- .../szyx/tcm/supervision/InsuranceCoreDemo.java | 11 ++ .../java/com/szyx/tcm/supervision/testDemo.java | 161 +++++++++++++++++++++ 6 files changed, 212 insertions(+), 7 deletions(-) create mode 100644 src/test/java/com/szyx/tcm/supervision/testDemo.java diff --git a/pom.xml b/pom.xml index 9013edb..ff2fd04 100644 --- a/pom.xml +++ b/pom.xml @@ -8,6 +8,8 @@ szyx-tcm-supervision szyx-tcm-supervision + jar + 1.8 @@ -207,7 +209,7 @@ com.tencent.mip med-request-data-sdk - 2.1.2 + 2.1.2-SNAPSHOT @@ -251,6 +253,13 @@ compile + + org.apache.httpcomponents + httpclient + 4.5.1 + + + @@ -330,7 +339,6 @@ ${spring-boot.version} com.szyx.tcm.supervision.SupervisionApplication - true diff --git a/src/main/java/com/szyx/tcm/supervision/config/HttpConfig.java b/src/main/java/com/szyx/tcm/supervision/config/HttpConfig.java index caa107e..d0935a8 100644 --- a/src/main/java/com/szyx/tcm/supervision/config/HttpConfig.java +++ b/src/main/java/com/szyx/tcm/supervision/config/HttpConfig.java @@ -4,6 +4,9 @@ import org.asynchttpclient.DefaultAsyncHttpClient; import org.asynchttpclient.DefaultAsyncHttpClientConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; /******************************************************************* @@ -28,8 +31,18 @@ public class HttpConfig { } @Bean - public RestTemplate restTemplate() { - return new RestTemplate(); + public RestTemplate restTemplate(ClientHttpRequestFactory factory) { + RestTemplate restTemplate = new RestTemplate(); + restTemplate.setRequestFactory(factory); + return restTemplate; + } + + @Bean + public ClientHttpRequestFactory clientHttpRequestFactory() { + SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); + factory.setReadTimeout(10000); + factory.setConnectTimeout(15000); + return factory; } diff --git a/src/main/java/com/szyx/tcm/supervision/controller/insurance/ProxyController.java b/src/main/java/com/szyx/tcm/supervision/controller/insurance/ProxyController.java index 99cd1ed..caada5d 100644 --- a/src/main/java/com/szyx/tcm/supervision/controller/insurance/ProxyController.java +++ b/src/main/java/com/szyx/tcm/supervision/controller/insurance/ProxyController.java @@ -1,10 +1,12 @@ package com.szyx.tcm.supervision.controller.insurance; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import javax.servlet.http.HttpServletRequest; @@ -24,6 +26,7 @@ import java.util.Objects; */ @RestController @RequestMapping("/proxy") +@Slf4j public class ProxyController { @Value("${insurance.host}") @@ -56,7 +59,16 @@ public class ProxyController { responseHeaders.setContentType(MediaType.APPLICATION_JSON); HttpEntity entity = new HttpEntity<>(body, headers); - ResponseEntity response = restTemplate.exchange(url, Objects.requireNonNull(HttpMethod.resolve(request.getMethod())), entity, String.class); + ResponseEntity response = null; + try { + log.info("Proxy request: {} {}", request.getMethod(), url); + log.info("Proxy request body: {}", body); + response = restTemplate.exchange(url, Objects.requireNonNull(HttpMethod.resolve(request.getMethod())), entity, String.class); + } catch (HttpClientErrorException e) { + String resBody = e.getResponseBodyAsString(); + log.error("Proxy request failed: {} {}", e.getStatusCode(), resBody); + return new ResponseEntity<>(resBody, responseHeaders, e.getStatusCode()); + } return new ResponseEntity<>(response.getBody(), responseHeaders,response.getStatusCode()); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index db5fc13..cb95f0c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ # \u5E94\u7528\u670D\u52A1 WEB \u8BBF\u95EE\u7AEF\u53E3 -server.port=9735 +server.port=9011 spring.profiles.active=${profile.active} swagger.enable=true server.http.encoding.force=true @@ -15,7 +15,7 @@ spring.mvc.pathmatch.matching-strategy=ant_path_matcher arthas.slient-init=true # \u533B\u4FDD\u4E13\u7F51\u914D\u7F6E -insurance.host =http://localhost:9345/ +insurance.host =http://10.85.254.46:8000/ ## \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014 mysql \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014 diff --git a/src/test/java/com/szyx/tcm/supervision/InsuranceCoreDemo.java b/src/test/java/com/szyx/tcm/supervision/InsuranceCoreDemo.java index 3325038..2525664 100644 --- a/src/test/java/com/szyx/tcm/supervision/InsuranceCoreDemo.java +++ b/src/test/java/com/szyx/tcm/supervision/InsuranceCoreDemo.java @@ -9,6 +9,7 @@ import com.szyx.tcm.supervision.model.dto.insurance.InsuranceCoreOutputMessage11 import com.szyx.tcm.supervision.model.dto.insurance.InsuranceUploadRecipeDetailInputMessage; import com.szyx.tcm.supervision.util.DateUtils; import com.szyx.tcm.supervision.util.InsuranceWechatUtil; +import com.szyx.tcm.supervision.util.SZYXAccessUtil; import com.tencent.mip.DataHandler; import com.tencent.mip.MIPayInvoker; import com.tencent.mip.model.DetailUploadReq; @@ -88,6 +89,16 @@ public class InsuranceCoreDemo { } + @Test + public void test2() throws Exception { + System.out.println("test2"); + long timestamp = System.currentTimeMillis(); + System.out.println(SZYXAccessUtil.encryptTimeStamp(timestamp)); + System.out.println(SZYXAccessUtil.encryptByAES(Long.toString(timestamp))); + + System.out.println( InsuranceWechatUtil.getMsgId("H41148100267")); + } + private void buildText(InsuranceUploadRecipeDetailInputMessage uploadData){ // 医保核心系统患者基本信息 diff --git a/src/test/java/com/szyx/tcm/supervision/testDemo.java b/src/test/java/com/szyx/tcm/supervision/testDemo.java new file mode 100644 index 0000000..819c906 --- /dev/null +++ b/src/test/java/com/szyx/tcm/supervision/testDemo.java @@ -0,0 +1,161 @@ +package com.szyx.tcm.supervision; + +import com.szyx.tcm.supervision.util.SZYXAccessUtil; +import org.apache.http.HttpEntity; +import org.apache.http.HttpStatus; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +/******************************************************************* + *

+ * @文件名称: testDemo.java
+ * @包 路  径: com.szyx.tcm.supervision
+ * @Copyright:wy (C) 2024 *
+ * @Description:
+ * @Version: V1.0
+ * @Author: wy
+ * @Date: 2024/7/28 15:34
+ * @Modify:
+ */
+public class testDemo {
+
+
+    private static final String url = "http://123.54.6.202:49336/szyx/api/insurancePayment/proxy/hss-call-service-interface";
+    /** 按照报文要求传入JSON格式字符串 */
+    private static final String downInput = "{…}";
+
+    /**
+     * 调用普通交易及文件下载交易
+     */
+    @Test
+    public void test1() {
+        CloseableHttpClient httpclient = HttpClients.createDefault();
+        HttpPost httppost = new HttpPost(url);
+        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build();
+        httppost.setConfig(requestConfig);
+        long timestamp = System.currentTimeMillis();
+        httppost.setHeader("timestamp", SZYXAccessUtil.encryptTimeStamp(timestamp));
+        httppost.setHeader("accessToken", SZYXAccessUtil.encryptByAES(Long.toString(timestamp)));
+        ByteArrayEntity entity = new ByteArrayEntity(downInput.getBytes(StandardCharsets.UTF_8));
+        entity.setContentType("text/plain");
+        httppost.setEntity(entity);
+        CloseableHttpResponse response = null;
+        try {
+            response = httpclient.execute(httppost);
+            int statusCode = response.getStatusLine().getStatusCode();
+            if (statusCode != HttpStatus.SC_OK) {
+                httppost.abort();
+                throw new RuntimeException("HttpClient,error status code :" + statusCode);
+            }
+            HttpEntity responseEntity = response.getEntity();
+            String result;
+            if (responseEntity != null) {
+                if (responseEntity.getContentType().getValue().contains("application/octet-stream")) {
+                    InputStream content = responseEntity.getContent();
+                    //返回文件流
+                    File file = new File("testDownload.txt");
+                    FileOutputStream fileOutputStream = new FileOutputStream(file);
+                    int temp;
+                    while ((temp = content.read()) != -1) {
+                        fileOutputStream.write(temp);
+                    }
+                    fileOutputStream.close();
+                } else {
+                    //返回字符串
+                    result = EntityUtils.toString(responseEntity, "UTF-8");
+                    System.out.println(result);
+                }
+            }
+            EntityUtils.consume(entity);
+        } catch (ClientProtocolException e) {
+            throw new RuntimeException("提交给服务器的请求,不符合HTTP协议", e);
+        } catch (IOException e) {
+            throw new RuntimeException("向服务器承保接口发起http请求,执行post请求异常", e);
+        } finally {
+            if (response != null) {
+                try {
+                    response.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (httpclient != null) {
+                try {
+                    httpclient.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+    /**
+     * 调用文件上传交易
+     */
+//    @Test
+//    public void test2() {
+//        File file = new File("testUpload.txt");
+//        CloseableHttpClient httpclient = HttpClients.createDefault();
+//        HttpPost httppost = new HttpPost(url);
+//        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build();
+//        httppost.setConfig(requestConfig);
+//        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
+//        builder.setCharset(StandardCharsets.UTF_8);
+//        builder.addTextBody("jsonStr", upInput);
+//        builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, "testUpload.txt");
+//        HttpEntity entity = builder.build();
+//        httppost.setEntity(entity);
+//        CloseableHttpResponse response = null;
+//        try {
+//            response = httpclient.execute(httppost);
+//            int statusCode = response.getStatusLine().getStatusCode();
+//            if (statusCode != HttpStatus.SC_OK) {
+//                httppost.abort();
+//                throw new RuntimeException("HttpClient,error status code :" + statusCode);
+//            }
+//            HttpEntity responseEntity = response.getEntity();
+//            String result;
+//            if (responseEntity != null) {
+//                //返回字符串
+//                result = EntityUtils.toString(responseEntity, "UTF-8");
+//                System.out.println(result);
+//            }
+//            EntityUtils.consume(entity);
+//        } catch (ClientProtocolException e) {
+//            throw new RuntimeException("提交给服务器的请求,不符合HTTP协议", e);
+//        } catch (IOException e) {
+//            throw new RuntimeException("向服务器承保接口发起http请求,执行post请求异常", e);
+//        } finally {
+//            if (response != null) {
+//                try {
+//                    response.close();
+//                } catch (IOException e) {
+//                    e.printStackTrace();
+//                }
+//            }
+//            if (httpclient != null) {
+//                try {
+//                    httpclient.close();
+//                } catch (IOException e) {
+//                    e.printStackTrace();
+//                }
+//            }
+//        }
+//    }
+
+
+
+}