Skip to content

Commit bca8a37

Browse files
committed
Java:MultiDataSource 为 APIAuto, UnitAuto, SQLAuto, UIGO 提供网页入口
1 parent 17b27b7 commit bca8a37

File tree

1 file changed

+70
-20
lines changed

1 file changed

+70
-20
lines changed

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

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import apijson.orm.JSONRequest;
8787
import apijson.orm.model.TestRecord;
8888
import apijson.router.APIJSONRouterController;
89+
import org.springframework.web.servlet.ModelAndView;
8990
import unitauto.MethodUtil;
9091

9192
import static apijson.RequestMethod.DELETE;
@@ -128,9 +129,30 @@ public class DemoController extends APIJSONRouterController<Long> { // APIJSONC
128129
// 可以更方便地通过日志排查错误
129130
@Override
130131
public String getRequestURL() {
131-
return httpServletRequest.getRequestURL().toString();
132+
HttpServletRequest httpReq = httpServletRequest;
133+
134+
String path = httpReq.getServletPath();
135+
int index = path.lastIndexOf("/");
136+
path = index < 0 ? path : path.substring(0, index);
137+
String host = httpReq.getHeader("origin");
138+
if (StringUtil.isEmpty(host)) {
139+
host = httpReq.getHeader("host");
140+
String prefix = httpReq.getProtocol().trim().toLowerCase().contains("https") ? "https://" : "http://";
141+
if (StringUtil.isEmpty(host)) {
142+
host = prefix + httpReq.getServerName() + ":" + httpReq.getServerPort();
143+
} else {
144+
host = prefix + host;
145+
}
146+
}
147+
148+
return host + path;
132149
}
133150

151+
// @Override
152+
// public Parser<Long> newParser(HttpSession session, RequestMethod method) {
153+
// return super.newParser(session, method).setNeedVerify(false);
154+
// }
155+
134156
/**增删改查统一的类 RESTful API 入口,牺牲一点路由解析性能来提升一些开发效率
135157
* @param method
136158
* @param tag
@@ -385,24 +407,9 @@ public String openGet(@PathVariable("request") String request, HttpSession sessi
385407
// 以下代码是为了方便调试,引导手动/自动跳转 http://apijson.cn/api 来测试接口
386408
String newUrl = null;
387409
try {
388-
HttpServletRequest httpReq = httpServletRequest;
410+
String url = getRequestURL();
389411

390-
String path = httpReq.getServletPath();
391-
int index = path.lastIndexOf("/");
392-
path = index < 0 ? path : path.substring(0, index);
393-
String host = httpReq.getHeader("origin");
394-
if (StringUtil.isEmpty(host)) {
395-
host = httpReq.getHeader("host");
396-
String prefix = httpReq.getProtocol().trim().toLowerCase().contains("https") ? "https://" : "http://";
397-
if (StringUtil.isEmpty(host)) {
398-
host = prefix + httpReq.getServerName() + ":" + httpReq.getServerPort();
399-
} else {
400-
host = prefix + host;
401-
}
402-
}
403-
404-
String url = host + path;
405-
String query = StringUtil.getTrimedString(httpReq.getQueryString());
412+
String query = StringUtil.getTrimedString(httpServletRequest.getQueryString());
406413
if (StringUtil.isNotEmpty(query)) {
407414
try {
408415
query = "?" + URLEncoder.encode(query, StandardCharsets.UTF_8);
@@ -412,7 +419,7 @@ public String openGet(@PathVariable("request") String request, HttpSession sessi
412419
}
413420
}
414421

415-
newUrl = "http://apijson.cn/api?send=false&redirect=false&type=JSON&unquote=true&url="
422+
newUrl = "http://apijson.cn/api?send=false&redirect=false&type=JSON&decode=true&url="
416423
+ url + query + "&json=" + request;
417424

418425
// httpServletResponse.setHeader("Referer", newUrl);
@@ -2724,6 +2731,50 @@ public String rapRepositoryDetail(@RequestParam("id") String id) {
27242731
// 为 APIAuto 提供的代理接口(解决跨域问题) 和 导入第三方文档的测试接口 https://github.com/TommyLemon/APIAuto https://github.com/TommyLemon/APIAuto >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
27252732

27262733

2734+
// 为 APIAuto, UnitAuto, SQLAuto, UIGO 提供网页入口 <<<<<<<<<<<<<<<<<<<<<<<<<<<
2735+
@GetMapping("api")
2736+
public String api() {
2737+
try {
2738+
httpServletResponse.sendRedirect("/api/index.html");
2739+
} catch (Throwable e) {
2740+
e.printStackTrace();
2741+
}
2742+
return "forward:/api/index.html"; // 两者都无效 "redirect:/api/index.html";
2743+
}
2744+
2745+
@GetMapping("unit")
2746+
public String unit() {
2747+
try {
2748+
httpServletResponse.sendRedirect("/unit/index.html");
2749+
} catch (Throwable e) {
2750+
e.printStackTrace();
2751+
}
2752+
return "forward:/unit/index.html"; // 两者都无效 "redirect:/unit/index.html";
2753+
}
2754+
2755+
@GetMapping("sql")
2756+
public String sql() {
2757+
try {
2758+
httpServletResponse.sendRedirect("/sql/index.html");
2759+
} catch (Throwable e) {
2760+
e.printStackTrace();
2761+
}
2762+
return "forward:/ui/index.html"; // 加载不了完整网页 return new ModelAndView("forward:/sql/index.html");
2763+
}
2764+
2765+
@GetMapping("ui")
2766+
public String ui() {
2767+
try {
2768+
httpServletResponse.sendRedirect("/ui/index.html");
2769+
} catch (Throwable e) {
2770+
e.printStackTrace();
2771+
}
2772+
return "forward:/ui/index.html"; // 两者都无效 "redirect:/ui/index.html";
2773+
}
2774+
2775+
// 为 APIAuto, UnitAuto, SQLAuto, UIGO 提供网页入口 >>>>>>>>>>>>>>>>>>>>>>>>>>>
2776+
2777+
27272778

27282779
// 为 UnitAuto 提供的单元测试接口 https://github.com/TommyLemon/UnitAuto <<<<<<<<<<<<<<<<<<<<<<<<<<<
27292780

@@ -2739,7 +2790,6 @@ public void invokeMethod(@RequestBody String request, HttpServletRequest servlet
27392790

27402791
// 为 UnitAuto 提供的单元测试接口 https://github.com/TommyLemon/UnitAuto >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
27412792

2742-
27432793
// 为 APIAuto, UnitAuto, SQLAuto 提供的后台 Headless 无 UI 测试转发接口 <<<<<<<<<<<<<<<<<<<<<<<<<<<
27442794

27452795
@GetMapping("api/test/start")

0 commit comments

Comments
 (0)