我有这个网络应用程序
@RestController
public class SomeController {
@GetMapping("/info")
public JsonResult<XXX> info() {
XXX xxx = // this urls's business
return JsonResult.success("info msg", xxx);
}
@GetMapping("/type")
public JsonResult<List<yyy>> type() {
List<yyy> yyyList = // this url's business
return JsonResult.success("type msg", yyyList);
}
}
现在,我想要一个这个网址
@GetMapping("/multi")
public JsonResult url(List<String> urls) {
// if urls=info,type
Map<String, Object> result = Maps.newHashMap();
for (String url : urls) {
// just GET method, other ignore
JsonResult jsonResult = handlerToUrl(url); // 1 to handler SC#info() 2 to SC#type()
if (jsonResult != null && jsonResult.getData() != null) {
result.put(url, jsonResult.getData());
}
}
return JsonResult.success("more info", result);
}
出现此问题是因为在应用(iOS,Android)中使用时,页面内容包含多个网址。
我正在尝试这样做
static final Map<String, List<String>> URL_MAP = Maps.newHashMap();
static {
URL_MAP.put("detail", Arrays.asList("SC#info", "SC#type"));
// etc.
}
@Autowired
private RequestMappingHandlerMapping mapping;
@GetMapping("/url")
public JsonResult url(String type) {
if (isBlank(type)) {
return JsonResult.fail("need some input");
}
List<String> urls = URL_MAP.get(type);
if (isBlank(urls)) {
return JsonResult.fail("no this page");
}
Map<String, Object> result = Maps.newHashMap();
for (String url : urls) {
// just first, other ignore
HandlerMethod handlerMethod = first(mapping.getHandlerMethodsForMappingName(url));
if (handlerMethod != null) {
Object urlResult = null;
try {
// XXX
urlResult = handlerMethod.getMethod().invoke(handlerMethod);
} catch (IllegalAccessException | InvocationTargetException e) {
// ignore
}
if (urlResult != null && urlResult instanceof JsonResult) {
Object data = ((JsonResult) urlResult).getData();
if (data != null) {
result.put(url.substring(url.indexOf("#") + 1), data);
}
}
}
}
return JsonResult.success("multi info", result);
}
但在// XXX行,我有这个执行
java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at xxx.UrlController.url(UrlController.java:40)
看起来不允许在spring mvc中调用内部调用中的方法,这会改变url请求的生命周期。
某些机构有一些好的建议吗?