Skip to content

Commit e4ab453

Browse files
author
yangzhao
committed
新增错误信息接口(ErrorInfo)
HandlerException构造传参ErrorInfo
1 parent 803aefa commit e4ab453

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.yz.common.core.enums;
2+
3+
/**
4+
* @author: yangzhao
5+
* @Date: 2020-01-05 13:57
6+
* @Description:
7+
*/
8+
public interface ErrorInfo {
9+
10+
public int getErrorCode();
11+
12+
public String getErrorMsg();
13+
}

common-core/src/main/java/com/yz/common/core/exception/HandlerException.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
package com.yz.common.core.exception;
22

3+
import com.yz.common.core.enums.ErrorInfo;
4+
35
/**
46
* Created by yangzhao on 17/3/29.
57
*/
68
public class HandlerException extends RuntimeException {
79

810
private int code;
911

10-
public HandlerException(int code,String message) {
12+
public HandlerException(int code, String message) {
1113
super(String.valueOf(message));
1214
this.code = code;
1315
}
16+
17+
public HandlerException(ErrorInfo errorInfo) {
18+
super(String.valueOf(errorInfo.getErrorMsg()));
19+
this.code = errorInfo.getErrorCode();
20+
}
21+
1422
public HandlerException(String message) {
1523
super(String.valueOf(message));
1624
}
1725

18-
public String getErrorInfo(){
26+
public String getErrorInfo() {
1927
return super.getMessage();
2028
}
2129

22-
public int getCode(){
30+
public int getCode() {
2331
return code;
2432
}
2533

common-core/src/main/java/com/yz/common/core/utils/HttpUtil.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ public static String sendPost(String url, String cnt, Map<String, String> header
146146
HttpPost httpPost = new HttpPost(url);
147147
StringEntity postEntity = new StringEntity(cnt, CHARSET_NAME);
148148
httpPost.setEntity(postEntity);
149-
httpPost.addHeader("Content-Type", "text/xml");
149+
httpPost.setHeader("Content-Type", "text/xml");
150150
HttpClient client = HttpClients.createDefault();
151151
if (headers != null && !headers.isEmpty()) {
152152
headers.forEach((k, v) -> {
153-
httpPost.addHeader(k, v);
153+
httpPost.setHeader(k, v);
154154
});
155155
}
156156
try {
@@ -171,6 +171,31 @@ public static String sendPost(String url, String cnt, Map<String, String> header
171171
return result;
172172
}
173173

174+
public static String sendJSONPost(String url, String jsonData, Map<String, String> headers) {
175+
String result = null;
176+
HttpPost httpPost = new HttpPost(url);
177+
StringEntity postEntity = new StringEntity(jsonData, CHARSET_NAME);
178+
postEntity.setContentType("application/json");
179+
httpPost.setEntity(postEntity);
180+
httpPost.setHeader("Content-Type", "application/json");
181+
HttpClient client = HttpClients.createDefault();
182+
if (headers != null && !headers.isEmpty()) {
183+
headers.forEach((k, v) -> {
184+
httpPost.setHeader(k, v);
185+
});
186+
}
187+
try {
188+
HttpResponse response = client.execute(httpPost);
189+
HttpEntity entity = response.getEntity();
190+
result = EntityUtils.toString(entity, "UTF-8");
191+
} catch (Exception e) {
192+
logger.error("http request error ", e);
193+
} finally {
194+
httpPost.abort();
195+
}
196+
return result;
197+
}
198+
174199
public static String escape(String src) {
175200
int i;
176201
char j;

0 commit comments

Comments
 (0)