@@ -1408,49 +1408,9 @@ else if (names != null) {
1408
1408
1409
1409
String rspBody = null ;
1410
1410
if (recordType >= 0 ) {
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
- }
1426
- }
1427
-
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
- }
1448
-
1449
- SESSION_MAP .put (session .getId (), session );
1450
- httpServletResponse .setHeader (APIJSON_DELEGATE_ID , session .getId ());
1411
+ rspBody = sendRequest (session , method , url , body , headers );
1451
1412
}
1452
1413
1453
-
1454
1414
if (recordType != 0 ) {
1455
1415
try {
1456
1416
boolean isBodyEmpty = StringUtil .isEmpty (body , true );
@@ -1755,43 +1715,65 @@ else if (recordType > 0) {
1755
1715
}
1756
1716
1757
1717
if (recordType < 0 ) {
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
- }
1769
- }
1718
+ rspBody = sendRequest (session , method , url , body , headers );
1719
+ }
1770
1720
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 ;
1721
+ return rspBody ;
1722
+ }
1723
+
1724
+ protected String sendRequest (HttpSession session , HttpMethod method , String url , String body , HttpHeaders headers ) {
1725
+ String rspBody = null ;
1726
+ try {
1727
+ RestTemplate client = new RestTemplate ();
1728
+ HttpEntity <String > requestEntity = new HttpEntity <>(method == HttpMethod .GET ? null : body , headers );
1729
+ ResponseEntity <String > entity = client .exchange (url , method , requestEntity , String .class );
1730
+
1731
+ HttpHeaders hhs = entity .getHeaders ();
1732
+ if (session != null && hhs != null ) {
1733
+ List <String > cookie = hhs .get (SET_COOKIE );
1734
+ if (cookie != null && cookie .isEmpty () == false ) {
1735
+ session .setAttribute (COOKIE , cookie );
1789
1736
}
1790
1737
}
1791
1738
1792
- SESSION_MAP .put (session .getId (), session );
1793
- httpServletResponse .setHeader (APIJSON_DELEGATE_ID , session .getId ());
1739
+ // FIXME 部分请求头导致 Response Body 未返回前端
1740
+ // Set<Entry<String, List<String>>> set = hhs.entrySet();
1741
+ // for (Entry<String, List<String>> e : set) {
1742
+ // String key = e == null ? null : e.getKey();
1743
+ // if (key != null) {
1744
+ // List<String> value = e.getValue();
1745
+ // httpServletResponse.setHeader(key, null);
1746
+ // for (String v : value) {
1747
+ // httpServletResponse.addHeader(key, v);
1748
+ // }
1749
+ // }
1750
+ // }
1751
+
1752
+ HttpStatus status = entity .getStatusCode ();
1753
+ httpServletResponse .setStatus (status .value (), status .getReasonPhrase ());
1754
+ rspBody = entity .getBody ();
1794
1755
}
1756
+ catch (Throwable e ) {
1757
+ try {
1758
+ if (e instanceof RestClientResponseException ) {
1759
+ RestClientResponseException hce = (RestClientResponseException ) e ;
1760
+ String msg = hce .getStatusText ();
1761
+ rspBody = hce .getResponseBodyAsString ();
1762
+ httpServletResponse .setStatus (hce .getRawStatusCode (), StringUtil .isEmpty (msg , true ) ? e .getMessage () : msg );
1763
+ } else {
1764
+ int code = CommonException .getCode (e );
1765
+ httpServletResponse .setStatus (code , e .getMessage ());
1766
+ }
1767
+ }
1768
+ catch (Throwable ex ) {
1769
+ int code = CommonException .getCode (e );
1770
+ httpServletResponse .setStatus (code , e .getMessage ());
1771
+ // throw e;
1772
+ }
1773
+ }
1774
+
1775
+ SESSION_MAP .put (session .getId (), session );
1776
+ httpServletResponse .setHeader (APIJSON_DELEGATE_ID , session .getId ());
1795
1777
1796
1778
return rspBody ;
1797
1779
}
0 commit comments