Skip to content

Commit e6ec77a

Browse files
authored
Merge pull request #198 from JiazhenBao/main
增值税发票识别和校验连接器请求异常打日志;httpclient异常处理优化
2 parents 59b409a + e38ab0e commit e6ec77a

7 files changed

Lines changed: 106 additions & 43 deletions

File tree

httpclient/README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# httpclient
22

33
简单http调用客户端。
4-
接口异常时,返回的IllegalArgumentException类型。ide中可在服务端逻辑中,根据返回类型是否是IllegalArgumentException判断接口是否执行成功。
4+
接口异常时,返回的TransferCommonException类型。ide中可在服务端逻辑中,根据返回类型是否是TransferCommonException判断接口是否执行成功。
5+
TransferCommonException中errorMsg包含了http状态码和返回信息。格式为:"httpStatus:" + httpStatus + ",responseBody:" + responseBody
56

67
# 接口详情
78
## LCAPHttpClient.exchange 数据格式为非form
@@ -12,7 +13,7 @@
1213
- header: 请求头
1314
- body: 请求体(Map<String,String>格式,不支持form)
1415

15-
出参:第三方返回完整信息的String格式
16+
出参:第三方返回完整信息的String格式 || TransferCommonException异常
1617

1718
## LCAPHttpClient.exchangeV2 数据格式为非form,异常时返回http错误码
1819
入参:
@@ -21,7 +22,7 @@
2122
- header: 请求头
2223
- body: 请求体(Map<String,String>格式,不支持form)
2324

24-
出参:第三方返回完整信息的String格式
25+
出参:第三方返回完整信息的String格式 || TransferCommonException异常
2526

2627
## LCAPHttpClient.exchangeWithoutUriEncode 数据格式为非form,url不编码
2728
入参:
@@ -30,7 +31,7 @@
3031
- header: 请求头
3132
- body: 请求体(Map<String,String>格式,不支持form)
3233

33-
出参:第三方返回完整信息的String格式
34+
出参:第三方返回完整信息的String格式 || TransferCommonException异常
3435

3536
## LCAPHttpClient.exchangeAllBodyType 数据格式为String(包括json序列化)。支持证书校验忽略
3637
入参:
@@ -41,7 +42,7 @@
4142
- body: 请求体(String格式,不支持文件。示例:a=1&b=cc&d=aasd1231)
4243
- isIgnoreCrt:是否忽略证书校验
4344

44-
出参:第三方返回完整信息的String格式
45+
出参:第三方返回完整信息的String格式 || TransferCommonException异常
4546

4647
## LCAPHttpClient.exchangeCrtForm https请求忽略证书,form表单专用body为MultiValueMap
4748
入参:
@@ -52,6 +53,8 @@
5253
- body: 请求体(Map<String,String>格式)
5354
- isIgnoreCrt:是否忽略证书校验
5455

56+
出参:第三方返回完整信息的String格式 || TransferCommonException异常
57+
5558
## LCAPHttpClient.exchangeCrt 数据格式为Map。支持证书校验忽略
5659
入参:
5760
- requestParam:
@@ -61,15 +64,15 @@
6164
- body: 请求体(Map<String,String>格式,不支持form)
6265
- isIgnoreCrt:是否忽略证书校验
6366

64-
出参:第三方返回完整信息的String格式
67+
出参:第三方返回完整信息的String格式 || TransferCommonException异常
6568

6669
## LCAPHttpClient.downloadFileUploadNos 下载文件,并上传到nos
6770
入参:
6871
- fileUrl: 请求地址
6972
- header: 请求头
7073
默认get请求,body为空
7174

72-
出参:上传到nos的文件地址
75+
出参:上传到nos的文件地址 || TransferCommonException异常
7376

7477
## LCAPHttpClient.uploadNosExchange 上传文件到nos,并调用第三方接口
7578
入参:
@@ -82,4 +85,4 @@
8285
- body: 请求体(Map<String,String>格式,仅支持form)
8386
- isIgnoreCrt:是否忽略证书校验
8487

85-
出参:第三方返回完整信息的String格式
88+
出参:第三方返回完整信息的String格式 || TransferCommonException异常

httpclient/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<artifactId>httpclient</artifactId>
1717
<name>httpclient</name>
1818
<description>httpclient</description>
19-
<version>0.14.8</version>
19+
<version>0.15.0</version>
2020

2121
<dependencies>
2222
<dependency>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.netease.http.exception;
2+
3+
public class TransferCommonException extends RuntimeException {
4+
private String message;
5+
6+
public TransferCommonException(int httpStatus, String responseBody) {
7+
this.message = "httpStatus:" + httpStatus + ",responseBody:" + responseBody;
8+
}
9+
10+
public TransferCommonException(String responseBody, Throwable t) {
11+
super(t);
12+
this.message = responseBody;
13+
}
14+
15+
16+
@Override
17+
public String getMessage() {
18+
return message;
19+
}
20+
21+
public void setMessage(String message) {
22+
this.message = message;
23+
}
24+
}

httpclient/src/main/java/com/netease/http/httpclient/LCAPHttpClient.java

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.alibaba.fastjson.JSONObject;
44
import com.netease.http.dto.*;
5+
import com.netease.http.exception.TransferCommonException;
56
import com.netease.http.util.FileUtil;
67
import com.netease.http.util.SSLUtil;
78
import com.netease.lowcode.core.annotation.NaslLogic;
@@ -61,7 +62,7 @@ public class LCAPHttpClient {
6162
@NaslLogic
6263
@Deprecated
6364
@Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000L))
64-
public String exchange(@Required String url, @Required String httpMethod, @Required Map<String, String> header, @Required Map<String, String> body) throws IllegalArgumentException {
65+
public String exchange(@Required String url, @Required String httpMethod, @Required Map<String, String> header, @Required Map<String, String> body) throws TransferCommonException {
6566
try {
6667
RequestParamAllBodyTypeInner requestParam = new RequestParamAllBodyTypeInner();
6768
requestParam.setBody(body);
@@ -73,14 +74,14 @@ public String exchange(@Required String url, @Required String httpMethod, @Requi
7374
if (exchange.getStatusCode() == HttpStatus.OK) {
7475
return exchange.getBody();
7576
} else {
76-
throw new IllegalArgumentException("请求http失败,返回:" + JSONObject.toJSONString(exchange));
77+
throw new TransferCommonException(exchange.getStatusCodeValue(), JSONObject.toJSONString(exchange));
7778
}
7879
} catch (HttpClientErrorException e) {
7980
logger.error("", e);
80-
throw new IllegalArgumentException(e.getResponseBodyAsString());
81+
throw new TransferCommonException(e.getStatusCode().value(), e.getResponseBodyAsString());
8182
} catch (Exception e) {
8283
logger.error("", e);
83-
throw new IllegalArgumentException(e.getMessage());
84+
throw new TransferCommonException(e.getMessage(), e);
8485
}
8586
}
8687

@@ -97,7 +98,7 @@ public String exchange(@Required String url, @Required String httpMethod, @Requi
9798
*/
9899
@NaslLogic
99100
@Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000L))
100-
public String exchangeV2(@Required String url, @Required String httpMethod, @Required Map<String, String> header, @Required Map<String, String> body) throws IllegalArgumentException {
101+
public String exchangeV2(@Required String url, @Required String httpMethod, @Required Map<String, String> header, @Required Map<String, String> body) throws TransferCommonException {
101102
try {
102103
RequestParamAllBodyTypeInner requestParam = new RequestParamAllBodyTypeInner();
103104
requestParam.setBody(body);
@@ -112,7 +113,7 @@ public String exchangeV2(@Required String url, @Required String httpMethod, @Req
112113
return e.getResponseBodyAsString();
113114
} catch (Exception e) {
114115
logger.error("", e);
115-
throw new IllegalArgumentException(e.getMessage());
116+
throw new TransferCommonException(e.getMessage(), e);
116117
}
117118
}
118119

@@ -126,7 +127,7 @@ public String exchangeV2(@Required String url, @Required String httpMethod, @Req
126127
* @return
127128
*/
128129
@NaslLogic
129-
public String downloadFileUploadNos(String fileUrl, String fileName, Map<String, String> header) throws IllegalArgumentException {
130+
public String downloadFileUploadNos(String fileUrl, String fileName, Map<String, String> header) throws TransferCommonException {
130131
File file = null;
131132
try {
132133
RequestParamAllBodyTypeInner requestParam = new RequestParamAllBodyTypeInner();
@@ -138,10 +139,10 @@ public String downloadFileUploadNos(String fileUrl, String fileName, Map<String,
138139
return uploadResponseDTO.result;
139140
} catch (HttpClientErrorException e) {
140141
logger.error("", e);
141-
throw new IllegalArgumentException(e.getResponseBodyAsString());
142+
throw new TransferCommonException(e.getStatusCode().value(), e.getResponseBodyAsString());
142143
} catch (Exception e) {
143144
logger.error("", e);
144-
throw new IllegalArgumentException(e.getMessage());
145+
throw new TransferCommonException(e.getMessage(), e);
145146
} finally {
146147
if (file != null && file.exists()) {
147148
file.delete();
@@ -158,7 +159,7 @@ public String downloadFileUploadNos(String fileUrl, String fileName, Map<String,
158159
* @return
159160
*/
160161
@NaslLogic
161-
public String uploadNosExchange(String fileUrl, String requestUrl, RequestParam requestParam) throws IllegalArgumentException {
162+
public String uploadNosExchange(String fileUrl, String requestUrl, RequestParam requestParam) throws TransferCommonException {
162163
File file = null;
163164
try {
164165
RequestParamAllBodyTypeInner requestParamGetFile = new RequestParamAllBodyTypeInner();
@@ -193,14 +194,14 @@ public String uploadNosExchange(String fileUrl, String requestUrl, RequestParam
193194
if (exchange.getStatusCode() == HttpStatus.OK) {
194195
return exchange.getBody();
195196
} else {
196-
throw new IllegalArgumentException("请求http失败,返回:" + JSONObject.toJSONString(exchange));
197+
throw new TransferCommonException(exchange.getStatusCodeValue(), JSONObject.toJSONString(exchange));
197198
}
198199
} catch (HttpClientErrorException e) {
199200
logger.error("", e);
200-
throw new IllegalArgumentException(e.getResponseBodyAsString());
201+
throw new TransferCommonException(e.getStatusCode().value(), e.getResponseBodyAsString());
201202
} catch (Exception e) {
202203
logger.error("", e);
203-
throw new IllegalArgumentException(e.getMessage());
204+
throw new TransferCommonException(e.getMessage(), e);
204205
} finally {
205206
if (file != null && file.exists()) {
206207
file.delete();
@@ -216,7 +217,7 @@ public String uploadNosExchange(String fileUrl, String requestUrl, RequestParam
216217
*/
217218
@NaslLogic
218219
@Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000L))
219-
public String exchangeCrt(RequestParam requestParam) throws IllegalArgumentException {
220+
public String exchangeCrt(RequestParam requestParam) throws TransferCommonException {
220221
try {
221222
if (requestParam.getIsIgnoreCrt() == null) {
222223
requestParam.setIsIgnoreCrt(false);
@@ -229,14 +230,14 @@ public String exchangeCrt(RequestParam requestParam) throws IllegalArgumentExcep
229230
if (exchange.getStatusCode() == HttpStatus.OK) {
230231
return exchange.getBody();
231232
} else {
232-
throw new IllegalArgumentException("请求http失败,返回:" + JSONObject.toJSONString(exchange));
233+
throw new TransferCommonException(exchange.getStatusCodeValue(), JSONObject.toJSONString(exchange));
233234
}
234235
} catch (HttpClientErrorException e) {
235236
logger.error("", e);
236-
throw new IllegalArgumentException(e.getResponseBodyAsString());
237+
throw new TransferCommonException(e.getStatusCode().value(), e.getResponseBodyAsString());
237238
} catch (Exception e) {
238239
logger.error("", e);
239-
throw new IllegalArgumentException(e.getMessage());
240+
throw new TransferCommonException(e.getMessage(), e);
240241
}
241242
}
242243

@@ -249,7 +250,7 @@ public String exchangeCrt(RequestParam requestParam) throws IllegalArgumentExcep
249250
*/
250251
@NaslLogic
251252
@Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000L))
252-
public String exchangeAllBodyType(RequestParamAllBodyType requestParam) throws IllegalArgumentException {
253+
public String exchangeAllBodyType(RequestParamAllBodyType requestParam) throws TransferCommonException {
253254
try {
254255
if (requestParam.getIsIgnoreCrt() == null) {
255256
requestParam.setIsIgnoreCrt(false);
@@ -263,14 +264,14 @@ public String exchangeAllBodyType(RequestParamAllBodyType requestParam) throws I
263264
if (exchange.getStatusCode() == HttpStatus.OK) {
264265
return exchange.getBody();
265266
} else {
266-
throw new IllegalArgumentException("请求http失败,返回:" + JSONObject.toJSONString(exchange));
267+
throw new TransferCommonException(exchange.getStatusCodeValue(), JSONObject.toJSONString(exchange));
267268
}
268269
} catch (HttpClientErrorException e) {
269270
logger.error("", e);
270-
throw new IllegalArgumentException(e.getResponseBodyAsString());
271+
throw new TransferCommonException(e.getStatusCode().value(), e.getResponseBodyAsString());
271272
} catch (Exception e) {
272273
logger.error("", e);
273-
throw new IllegalArgumentException(e.getMessage());
274+
throw new TransferCommonException(e.getMessage(), e);
274275
}
275276
}
276277

@@ -282,7 +283,7 @@ public String exchangeAllBodyType(RequestParamAllBodyType requestParam) throws I
282283
*/
283284
@NaslLogic
284285
@Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000L))
285-
public String exchangeCrtForm(RequestParam requestParam) throws IllegalArgumentException {
286+
public String exchangeCrtForm(RequestParam requestParam) throws TransferCommonException {
286287
try {
287288
if (requestParam.getIsIgnoreCrt() == null) {
288289
requestParam.setIsIgnoreCrt(false);
@@ -302,14 +303,14 @@ public String exchangeCrtForm(RequestParam requestParam) throws IllegalArgumentE
302303
if (exchange.getStatusCode() == HttpStatus.OK) {
303304
return exchange.getBody();
304305
} else {
305-
throw new IllegalArgumentException("请求http失败,返回:" + JSONObject.toJSONString(exchange));
306+
throw new TransferCommonException(exchange.getStatusCode().value(), JSONObject.toJSONString(exchange));
306307
}
307308
} catch (HttpClientErrorException e) {
308309
logger.error("", e);
309-
throw new IllegalArgumentException(e.getResponseBodyAsString());
310+
throw new TransferCommonException(e.getStatusCode().value(), e.getResponseBodyAsString());
310311
} catch (Exception e) {
311312
logger.error("", e);
312-
throw new IllegalArgumentException(e.getMessage());
313+
throw new TransferCommonException(e.getMessage(), e);
313314
}
314315
}
315316

@@ -325,7 +326,7 @@ public String exchangeCrtForm(RequestParam requestParam) throws IllegalArgumentE
325326
*/
326327
@NaslLogic
327328
@Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000L))
328-
public String exchangeWithoutUriEncode(@Required String url, @Required String httpMethod, @Required Map<String, String> header, @Required Map<String, String> body) throws IllegalArgumentException {
329+
public String exchangeWithoutUriEncode(@Required String url, @Required String httpMethod, @Required Map<String, String> header, @Required Map<String, String> body) throws TransferCommonException {
329330
try {
330331
RequestParamAllBodyTypeInner requestParam = new RequestParamAllBodyTypeInner();
331332
requestParam.setBody(body);
@@ -337,14 +338,14 @@ public String exchangeWithoutUriEncode(@Required String url, @Required String ht
337338
if (exchange.getStatusCode() == HttpStatus.OK) {
338339
return exchange.getBody();
339340
} else {
340-
throw new IllegalArgumentException("请求http失败,返回:" + JSONObject.toJSONString(exchange));
341+
throw new TransferCommonException(exchange.getStatusCodeValue(), JSONObject.toJSONString(exchange));
341342
}
342343
} catch (HttpClientErrorException e) {
343344
logger.error("", e);
344-
throw new IllegalArgumentException(e.getResponseBodyAsString());
345+
throw new TransferCommonException(e.getStatusCode().value(), e.getResponseBodyAsString());
345346
} catch (Exception e) {
346347
logger.error("", e);
347-
throw new IllegalArgumentException(e.getMessage());
348+
throw new TransferCommonException(e.getMessage(), e);
348349
}
349350
}
350351
}

httpclient/src/test/java/HttpTest.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import com.netease.http.config.NosConfig;
21
import com.netease.http.dto.RequestParam;
32
import com.netease.http.httpclient.HttpClientService;
43
import com.netease.http.httpclient.LCAPHttpClient;
@@ -33,17 +32,49 @@ public class HttpTest {
3332

3433
// @Test
3534
public void testV2() {
36-
String url = "";
35+
String url = "http://127.0.0.1:8090/expand/transfer/get_result?key=1";
3736
Map<String, String> head = new HashMap<>();
3837
head.put("Content-Type", "application/json");
3938
head.put("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
4039
Map<String, String> body = new HashMap<>();
4140
body.put("userName", "222");
4241
body.put("password", "11");
42+
String urlRes = lcapHttpClient.exchangeV2(url, HttpMethod.GET.name(), head, body);
43+
System.out.println(urlRes);
44+
}
45+
46+
// @Test
47+
public void testV22() {
48+
String url = "http://127.0.0.1:8090/api/office_to_pdf/pptToPdf";
49+
Map<String, String> head = new HashMap<>();
50+
head.put("Content-Type", "application/json");
51+
head.put("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
52+
Map<String, String> body = new HashMap<>();
53+
body.put("fileUrl", "http://dev.bjztest.neteasepmo.lcap.163yun.com/upload/app/06889c61-5b85-4b8a-9eac-8d14f9159f8a/aaaa-50%E9%A1%B5_20240730105501059.pptx");
54+
body.put("type", "1");
4355
String urlRes = lcapHttpClient.exchangeV2(url, HttpMethod.POST.name(), head, body);
4456
System.out.println(urlRes);
4557
}
4658

59+
// @Test
60+
public void testExchangeCrtForm() {
61+
RequestParam requestParam = new RequestParam();
62+
String url = "https://test.sovell.com/single-demeter/merger/api/operator/login";
63+
Map<String, String> head = new HashMap<>();
64+
head.put("Content-Type", "multipart/form-data");
65+
Map<String, String> body = new HashMap<>();
66+
body.put("username", "18888888888");
67+
body.put("password", "zMEidaK9kEra6Ub1ptl2Sw==");
68+
body.put("platform", "single_demeter");
69+
70+
requestParam.setUrl(url);
71+
requestParam.setHttpMethod("get");
72+
// requestParam.setHeader(head);
73+
requestParam.setBody(body);
74+
String urlRes = lcapHttpClient.exchangeCrtForm(requestParam);
75+
System.out.println(urlRes);
76+
}
77+
4778

4879
// @Test
4980
public void testHttpBody() throws URISyntaxException {
@@ -69,7 +100,7 @@ public void testHttpBody() throws URISyntaxException {
69100
// System.out.println(url);
70101
}
71102

72-
// @Test
103+
// @Test
73104
public void testDownload() {
74105
URI uri = UriComponentsBuilder
75106
.fromUriString("http://www.baidu.com")

ocrinvoice_fdddf/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<artifactId>ocrinvoice</artifactId>
99
<name>增值税发票识别和校验</name>
1010
<description>增值税发票识别、增值税发票核验、增值税发票(卷票)识别</description>
11-
<version>1.0.6</version>
11+
<version>1.0.7</version>
1212

1313
<properties>
1414
<maven.compiler.source>8</maven.compiler.source>

0 commit comments

Comments
 (0)