michaelliao / summer-framework Goto Github PK
View Code? Open in Web Editor NEWSummer framework is a minimal Spring framework.
License: GNU General Public License v3.0
Summer framework is a minimal Spring framework.
License: GNU General Public License v3.0
非常感谢廖老师采纳了我的上一个Issue
本人学艺不精,在实现MVC部分又产生了一些疑问,希望廖老师能拨冗解答
简而言之是Summer框架对@RestController和@responsebody注解的处理似乎与Spring不尽相同。
在下面的这段代码中(节选自Dispatcher.java),您对同时具有上述两个注解的结果是采取了直接以字符流或字节流输出到resp的方式,似乎并未考虑结果为Java对象时的情况,结合测试部分代码才发现您在标有@responsebody注解的方法中已经先将Java对象转换为JSON格式之后再返回的,尽管能自洽但似乎不够优雅(学艺不精,无意冒犯)。
反观Spring的处理方式,是将@RestController当做@controller+@responsebody来用,即二者的功能和处理手段是相同的,使用者无需在Controller方法的返回值中手动将Java对象转为JSON格式,而是像您这里对没有@responsebody注解的结果处理方式一样,直接返回Java对象,由框架统一转JSON格式
for (Dispatcher dispatcher : dispatchers) {
Result result = dispatcher.process(url, req, resp);
if (result.processed()) {
Object r = result.returnObject();
if (dispatcher.isRest) {
// send rest response:
if (!resp.isCommitted()) {
resp.setContentType("application/json");
}
if (dispatcher.isResponseBody) {
if (r instanceof String s) {
// send as response body:
PrintWriter pw = resp.getWriter();
pw.write(s);
pw.flush();
} else if (r instanceof byte[] data) {
// send as response body:
ServletOutputStream output = resp.getOutputStream();
output.write(data);
output.flush();
} else {
// error:
throw new ServletException("Unable to process REST result when handle url: " + url);
}
} else if (!dispatcher.isVoid) {
PrintWriter pw = resp.getWriter();
JsonUtils.writeJson(pw, r);
pw.flush();
}
}
因此我的建议是与Spring处理方式一致,若有@RestController注解则不再判断是否有@responsebody注解,统一转JSON格式写入
再次再次说明,本人学艺不精,如有胡言乱语之处,请廖老师直接忽视即可~
DispatcherServlet类的有参构造器中PropertyResolver参数命名错误,源代码为:
public DispatcherServlet(ApplicationContext applicationContext, PropertyResolver properyResolver)
显然这里第二个参数应为“propertyResolver”,代码中少写了一个字母“t”,虽然对代码运行无影响,但对本项目的严谨性稍有影响,希望可以更正,维持本项目的优雅与完美
(同时非常感谢廖老师这个项目,真的让我学习到了很多~)
我可能没表达清楚,从用户的角度来说@responsebody注解对实际工作似乎没有太大作用啊,如果用户想返回java对象还需要手动调用JsonUtils.writeJson()方法将对象转换为JSON格式,那用户使用这个注解的意义在哪里呢?仅仅起到了校验返回值是否为String/byte[]的作用?
从我的角度理解,@responsebody注解应该实现的功能是,如果用户返回的是java对象,则检测到该注解后Summer框架将对象转换为JSON格式并写入HttpServletResponse,就和@RestController的功能相同,唯一区别是该注解应用于Method,实际Spring中这个注解的功能也是如此。
Originally posted by @ChenPi222 in #12 (comment)
ResourceResolver中的scan0函数里,使用String uriStr = removeTrailingSlash(uri.toString());获取uri字符串,但是如果路径中包含特殊字符,比如说空格,那么这种方法解析出来的字符串空格会变成%20,字符串的长度变了,导致后续scanFile里获取的类名有问题。
我将其修改为String uriStr = removeTrailingSlash(URLDecoder.decode(uri.toString(), StandardCharsets.UTF_8));后,发现可以规避这个现象。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.