Skip to content

Commit 105cff2

Browse files
committed
Java:openGet /get/{request} 引导手动/自动跳转 {原地址}/api 来测试接口,解决参数未转义导致 APIAuto 报错
1 parent f6166a9 commit 105cff2

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

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

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,34 @@
126126
public class DemoController extends APIJSONRouterController<Long> { // APIJSONController<Long> {
127127
private static final String TAG = "DemoController";
128128

129-
// 可以更方便地通过日志排查错误
130-
@Override
131-
public String getRequestURL() {
129+
public String getRequestBaseURL() {
132130
HttpServletRequest httpReq = httpServletRequest;
133-
134-
String path = httpReq.getServletPath();
135-
int index = path.lastIndexOf("/");
136-
path = index < 0 ? path : path.substring(0, index);
137131
String host = httpReq.getHeader("origin");
132+
138133
if (StringUtil.isEmpty(host)) {
139134
host = httpReq.getHeader("host");
140135
String prefix = httpReq.getProtocol().trim().toLowerCase().contains("https") ? "https://" : "http://";
141136
if (StringUtil.isEmpty(host)) {
142-
host = prefix + httpReq.getServerName() + ":" + httpReq.getServerPort();
143-
} else {
144-
host = prefix + host;
137+
return prefix + httpReq.getServerName() + ":" + httpReq.getServerPort();
145138
}
139+
140+
return prefix + host;
146141
}
147142

148-
return host + path;
143+
return host;
144+
}
145+
146+
public String getRequestPath() {
147+
String path = httpServletRequest.getServletPath();
148+
int index = path.lastIndexOf("/");
149+
path = index < 0 ? path : path.substring(0, index);
150+
return path;
151+
}
152+
153+
// 可以更方便地通过日志排查错误
154+
@Override
155+
public String getRequestURL() {
156+
return getRequestBaseURL() + getRequestPath();
149157
}
150158

151159
// @Override
@@ -394,13 +402,13 @@ public String deleteByTag(@PathVariable("tag") String tag, @RequestParam Map<Str
394402
*/
395403
@GetMapping("get/{request}")
396404
public String openGet(@PathVariable("request") String request, HttpSession session) {
397-
if (! Log.DEBUG) { // 一般情况这样简单使用
398-
try {
399-
request = URLDecoder.decode(request, StandardCharsets.UTF_8);
400-
} catch (Exception e) {
401-
// Parser会报错
402-
}
405+
try {
406+
request = URLDecoder.decode(request, StandardCharsets.UTF_8);
407+
} catch (Exception e) {
408+
// Parser 会报错
409+
}
403410

411+
if (! Log.DEBUG) { // 一般情况这样简单使用
404412
return get(request, session);
405413
}
406414

@@ -419,8 +427,26 @@ public String openGet(@PathVariable("request") String request, HttpSession sessi
419427
}
420428
}
421429

422-
newUrl = "http://apijson.cn/api?send=false&redirect=false&type=JSON&decode=true&url="
423-
+ url + query + "&json=" + request;
430+
if (StringUtil.isNotEmpty(url)) {
431+
try {
432+
url = URLEncoder.encode(url, StandardCharsets.UTF_8);
433+
} catch (Throwable e) {
434+
e.printStackTrace();
435+
}
436+
}
437+
438+
String req = request;
439+
if (StringUtil.isNotEmpty(req)) {
440+
try {
441+
req = URLEncoder.encode(req, StandardCharsets.UTF_8);
442+
} catch (Throwable e) {
443+
e.printStackTrace();
444+
}
445+
}
446+
447+
String baseUrl = getRequestBaseURL();
448+
newUrl = baseUrl + "/api/index.html?send=false&redirect=false&type=JSON&decode=true&url="
449+
+ url + query + "&json=" + req;
424450

425451
// httpServletResponse.setHeader("Referer", newUrl);
426452
// httpServletResponse.setHeader("Redirect-Ref", newUrl);
@@ -429,12 +455,6 @@ public String openGet(@PathVariable("request") String request, HttpSession sessi
429455
e.printStackTrace();
430456
}
431457

432-
try {
433-
request = URLDecoder.decode(request, StandardCharsets.UTF_8);
434-
} catch (Exception e) {
435-
// Parser会报错
436-
}
437-
438458
JSONObject rsp = newParser(session, GET).parseResponse(request);
439459
rsp.put("@link", newUrl);
440460
String str = JSON.format(rsp); // rsp.toJSONString();

0 commit comments

Comments
 (0)