|
14 | 14 |
|
15 | 15 | package apijson.boot;
|
16 | 16 |
|
| 17 | +import apijson.orm.AbstractParser; |
| 18 | +import apijson.orm.exception.*; |
17 | 19 | import com.alibaba.fastjson.JSONArray;
|
18 | 20 | import com.alibaba.fastjson.JSONObject;
|
19 | 21 | import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
44 | 46 | import org.springframework.web.bind.annotation.RequestMapping;
|
45 | 47 | import org.springframework.web.bind.annotation.RequestParam;
|
46 | 48 | import org.springframework.web.bind.annotation.RestController;
|
| 49 | +import org.springframework.web.client.HttpClientErrorException; |
| 50 | +import org.springframework.web.client.RestClientResponseException; |
47 | 51 | import org.springframework.web.client.RestTemplate;
|
48 | 52 |
|
49 | 53 | import java.io.IOException;
|
|
57 | 61 | import java.util.*;
|
58 | 62 | import java.util.Map.Entry;
|
59 | 63 | import java.util.concurrent.TimeoutException;
|
60 |
| -import java.util.function.BiConsumer; |
61 | 64 |
|
62 | 65 | import javax.servlet.http.HttpServletRequest;
|
63 | 66 | import javax.servlet.http.HttpServletResponse;
|
|
78 | 81 | import apijson.demo.model.Verify;
|
79 | 82 | import apijson.framework.BaseModel;
|
80 | 83 | 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; |
85 | 84 | import apijson.orm.model.TestRecord;
|
86 | 85 | import apijson.router.APIJSONRouterController;
|
87 | 86 | import unitauto.MethodUtil;
|
@@ -1409,23 +1408,43 @@ else if (names != null) {
|
1409 | 1408 |
|
1410 | 1409 | String rspBody = null;
|
1411 | 1410 | 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 | + } |
1425 | 1426 | }
|
1426 |
| - } |
1427 | 1427 |
|
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 | + } |
1429 | 1448 |
|
1430 | 1449 | SESSION_MAP.put(session.getId(), session);
|
1431 | 1450 | httpServletResponse.setHeader(APIJSON_DELEGATE_ID, session.getId());
|
@@ -1736,19 +1755,39 @@ else if (recordType > 0) {
|
1736 | 1755 | }
|
1737 | 1756 |
|
1738 | 1757 | 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 | + } |
1748 | 1769 | }
|
1749 |
| - } |
1750 | 1770 |
|
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 | + } |
1752 | 1791 |
|
1753 | 1792 | SESSION_MAP.put(session.getId(), session);
|
1754 | 1793 | httpServletResponse.setHeader(APIJSON_DELEGATE_ID, session.getId());
|
|
0 commit comments