Skip to content

Commit 17b27b7

Browse files
committed
Java:openGet /get/{request} 引导手动/自动跳转 http://apijson.cn/api 来测试接口
1 parent aba5448 commit 17b27b7

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

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

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
import java.io.IOException;
5555
import java.lang.reflect.Array;
5656
import java.net.URLDecoder;
57+
import java.net.URLEncoder;
58+
import java.nio.charset.StandardCharsets;
5759
import java.rmi.ServerException;
5860
import java.sql.PreparedStatement;
5961
import java.sql.ResultSet;
@@ -370,12 +372,84 @@ public String deleteByTag(@PathVariable("tag") String tag, @RequestParam Map<Str
370372
*/
371373
@GetMapping("get/{request}")
372374
public String openGet(@PathVariable("request") String request, HttpSession session) {
375+
if (! Log.DEBUG) { // 一般情况这样简单使用
376+
try {
377+
request = URLDecoder.decode(request, StandardCharsets.UTF_8);
378+
} catch (Exception e) {
379+
// Parser会报错
380+
}
381+
382+
return get(request, session);
383+
}
384+
385+
// 以下代码是为了方便调试,引导手动/自动跳转 http://apijson.cn/api 来测试接口
386+
String newUrl = null;
373387
try {
374-
request = URLDecoder.decode(request, StringUtil.UTF_8);
388+
HttpServletRequest httpReq = httpServletRequest;
389+
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());
406+
if (StringUtil.isNotEmpty(query)) {
407+
try {
408+
query = "?" + URLEncoder.encode(query, StandardCharsets.UTF_8);
409+
} catch (Throwable e) {
410+
e.printStackTrace();
411+
query = "&" + query;
412+
}
413+
}
414+
415+
newUrl = "http://apijson.cn/api?send=false&redirect=false&type=JSON&unquote=true&url="
416+
+ url + query + "&json=" + request;
417+
418+
// httpServletResponse.setHeader("Referer", newUrl);
419+
// httpServletResponse.setHeader("Redirect-Ref", newUrl);
420+
// httpServletResponse.sendRedirect(newUrl);
421+
} catch (Throwable e) {
422+
e.printStackTrace();
423+
}
424+
425+
try {
426+
request = URLDecoder.decode(request, StandardCharsets.UTF_8);
375427
} catch (Exception e) {
376428
// Parser会报错
377429
}
378-
return get(request, session);
430+
431+
JSONObject rsp = newParser(session, GET).parseResponse(request);
432+
rsp.put("@link", newUrl);
433+
String str = JSON.format(rsp); // rsp.toJSONString();
434+
if (StringUtil.isEmpty(newUrl) || Math.random() < 0.5) {
435+
return str;
436+
}
437+
438+
String html = "<!DOCTYPE html>\n" +
439+
"<html>\n" +
440+
"<head>\n" +
441+
" <title>APIAuto-机器学习零代码测试、生成代码与静态检查、生成文档与光标悬浮注释</title>\n" +
442+
" <meta charset=\"utf-8\" />\n" +
443+
"</head>\n" +
444+
"\n" +
445+
"<body>\n" +
446+
" <span style=\"white-space: pre\">" + str + "</span>\n" +
447+
" <script language=\"javascript\">setTimeout(function() {\n" +
448+
" window.open('" + newUrl.replaceAll("'", "\\\\'") + "') \n" +
449+
" }, 5000)</script>\n" +
450+
"</body>";
451+
452+
return html;
379453
}
380454

381455
/**计数

0 commit comments

Comments
 (0)