Skip to content

Commit 06b5298

Browse files
committed
代理接口:解决 /delegate 请求报错时返回的总是 500 而不是实际值
1 parent a78b709 commit 06b5298

File tree

1 file changed

+70
-31
lines changed

1 file changed

+70
-31
lines changed

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/DemoController.java

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package apijson.boot;
1616

17+
import apijson.orm.AbstractParser;
18+
import apijson.orm.exception.*;
1719
import com.alibaba.fastjson.JSONArray;
1820
import com.alibaba.fastjson.JSONObject;
1921
import com.alibaba.fastjson.serializer.SerializerFeature;
@@ -44,6 +46,8 @@
4446
import org.springframework.web.bind.annotation.RequestMapping;
4547
import org.springframework.web.bind.annotation.RequestParam;
4648
import org.springframework.web.bind.annotation.RestController;
49+
import org.springframework.web.client.HttpClientErrorException;
50+
import org.springframework.web.client.RestClientResponseException;
4751
import org.springframework.web.client.RestTemplate;
4852

4953
import java.io.IOException;
@@ -57,7 +61,6 @@
5761
import java.util.*;
5862
import java.util.Map.Entry;
5963
import java.util.concurrent.TimeoutException;
60-
import java.util.function.BiConsumer;
6164

6265
import javax.servlet.http.HttpServletRequest;
6366
import javax.servlet.http.HttpServletResponse;
@@ -78,10 +81,6 @@
7881
import apijson.demo.model.Verify;
7982
import apijson.framework.BaseModel;
8083
import apijson.orm.JSONRequest;
81-
import apijson.orm.exception.ConditionErrorException;
82-
import apijson.orm.exception.ConflictException;
83-
import apijson.orm.exception.NotExistException;
84-
import apijson.orm.exception.OutOfRangeException;
8584
import apijson.orm.model.TestRecord;
8685
import apijson.router.APIJSONRouterController;
8786
import unitauto.MethodUtil;
@@ -1409,23 +1408,43 @@ else if (names != null) {
14091408

14101409
String rspBody = null;
14111410
if (recordType >= 0) {
1412-
RestTemplate client = new RestTemplate();
1413-
// 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
1414-
HttpEntity<String> requestEntity = new HttpEntity<>(method == HttpMethod.GET ? null : body, headers);
1415-
// 执行HTTP请求,这里可能抛异常,不要包装,直接让它抛,能够在浏览器 Console/XHR/{i}/Preview
1416-
// 看到 error: "Internal Server Error" message: "405 null" 之类的包括信息,
1417-
// 包装后反而容易混淆,并且会因为 JSON 结构不一致导致解析问题
1418-
ResponseEntity<String> entity = client.exchange(url, method, requestEntity, String.class);
1419-
1420-
HttpHeaders hhs = entity.getHeaders();
1421-
if (session != null && hhs != null) {
1422-
List<String> cookie = hhs.get(SET_COOKIE);
1423-
if (cookie != null && cookie.isEmpty() == false) {
1424-
session.setAttribute(COOKIE, cookie);
1411+
try {
1412+
RestTemplate client = new RestTemplate();
1413+
// 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
1414+
HttpEntity<String> requestEntity = new HttpEntity<>(method == HttpMethod.GET ? null : body, headers);
1415+
// 执行HTTP请求,这里可能抛异常,不要包装,直接让它抛,能够在浏览器 Console/XHR/{i}/Preview
1416+
// 看到 error: "Internal Server Error" message: "405 null" 之类的包括信息,
1417+
// 包装后反而容易混淆,并且会因为 JSON 结构不一致导致解析问题
1418+
ResponseEntity<String> entity = client.exchange(url, method, requestEntity, String.class);
1419+
1420+
HttpHeaders hhs = entity.getHeaders();
1421+
if (session != null && hhs != null) {
1422+
List<String> cookie = hhs.get(SET_COOKIE);
1423+
if (cookie != null && cookie.isEmpty() == false) {
1424+
session.setAttribute(COOKIE, cookie);
1425+
}
14251426
}
1426-
}
14271427

1428-
rspBody = entity.getBody();
1428+
HttpStatus status = entity.getStatusCode();
1429+
httpServletResponse.setStatus(status.value(), status.getReasonPhrase());
1430+
rspBody = entity.getBody();
1431+
}
1432+
catch (Throwable e) {
1433+
try {
1434+
if (e instanceof RestClientResponseException) {
1435+
RestClientResponseException hce = (RestClientResponseException) e;
1436+
String msg = hce.getStatusText();
1437+
httpServletResponse.sendError(hce.getRawStatusCode(), StringUtil.isEmpty(msg, true) ? e.getMessage() : msg);
1438+
} else {
1439+
int code = CommonException.getCode(e);
1440+
httpServletResponse.sendError(code, e.getMessage());
1441+
}
1442+
}
1443+
catch (Throwable ex) {
1444+
// httpServletResponse.setStatus(500, e.getMessage());
1445+
throw e;
1446+
}
1447+
}
14291448

14301449
SESSION_MAP.put(session.getId(), session);
14311450
httpServletResponse.setHeader(APIJSON_DELEGATE_ID, session.getId());
@@ -1736,19 +1755,39 @@ else if (recordType > 0) {
17361755
}
17371756

17381757
if (recordType < 0) {
1739-
RestTemplate client = new RestTemplate();
1740-
HttpEntity<String> requestEntity = new HttpEntity<>(method == HttpMethod.GET ? null : body, headers);
1741-
ResponseEntity<String> entity = client.exchange(url, method, requestEntity, String.class);
1742-
1743-
HttpHeaders hhs = entity.getHeaders();
1744-
if (session != null && hhs != null) {
1745-
List<String> cookie = hhs.get(SET_COOKIE);
1746-
if (cookie != null && cookie.isEmpty() == false) {
1747-
session.setAttribute(COOKIE, cookie);
1758+
try {
1759+
RestTemplate client = new RestTemplate();
1760+
HttpEntity<String> requestEntity = new HttpEntity<>(method == HttpMethod.GET ? null : body, headers);
1761+
ResponseEntity<String> entity = client.exchange(url, method, requestEntity, String.class);
1762+
1763+
HttpHeaders hhs = entity.getHeaders();
1764+
if (session != null && hhs != null) {
1765+
List<String> cookie = hhs.get(SET_COOKIE);
1766+
if (cookie != null && cookie.isEmpty() == false) {
1767+
session.setAttribute(COOKIE, cookie);
1768+
}
17481769
}
1749-
}
17501770

1751-
rspBody = entity.getBody();
1771+
HttpStatus status = entity.getStatusCode();
1772+
httpServletResponse.setStatus(status.value(), status.getReasonPhrase());
1773+
rspBody = entity.getBody();
1774+
}
1775+
catch (Throwable e) {
1776+
try {
1777+
if (e instanceof RestClientResponseException) {
1778+
RestClientResponseException hce = (RestClientResponseException) e;
1779+
String msg = hce.getStatusText();
1780+
httpServletResponse.sendError(hce.getRawStatusCode(), StringUtil.isEmpty(msg, true) ? e.getMessage() : msg);
1781+
} else {
1782+
int code = CommonException.getCode(e);
1783+
httpServletResponse.sendError(code, e.getMessage());
1784+
}
1785+
}
1786+
catch (Throwable ex) {
1787+
// httpServletResponse.setStatus(500, e.getMessage());
1788+
throw e;
1789+
}
1790+
}
17521791

17531792
SESSION_MAP.put(session.getId(), session);
17541793
httpServletResponse.setHeader(APIJSON_DELEGATE_ID, session.getId());

0 commit comments

Comments
 (0)