Coder Social home page Coder Social logo

summer-framework's Issues

对于web-实现MVC部分@RestController和@ResponseBody注解处理的疑问

非常感谢廖老师采纳了我的上一个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格式写入
再次再次说明,本人学艺不精,如有胡言乱语之处,请廖老师直接忽视即可~

summer-web板块中DispatcherServlet.java文件的参数命名错误

DispatcherServlet类的有参构造器中PropertyResolver参数命名错误,源代码为:
public DispatcherServlet(ApplicationContext applicationContext, PropertyResolver properyResolver)
显然这里第二个参数应为“propertyResolver”,代码中少写了一个字母“t”,虽然对代码运行无影响,但对本项目的严谨性稍有影响,希望可以更正,维持本项目的优雅与完美

(同时非常感谢廖老师这个项目,真的让我学习到了很多~)

对于web-实现MVC部分@ResponseBody注解功能的疑问

我可能没表达清楚,从用户的角度来说@responsebody注解对实际工作似乎没有太大作用啊,如果用户想返回java对象还需要手动调用JsonUtils.writeJson()方法将对象转换为JSON格式,那用户使用这个注解的意义在哪里呢?仅仅起到了校验返回值是否为String/byte[]的作用?
从我的角度理解,@responsebody注解应该实现的功能是,如果用户返回的是java对象,则检测到该注解后Summer框架将对象转换为JSON格式并写入HttpServletResponse,就和@RestController的功能相同,唯一区别是该注解应用于Method,实际Spring中这个注解的功能也是如此。

Originally posted by @ChenPi222 in #12 (comment)

ResourceResolver解析的路径包含空格或其他特殊字符导致的问题

ResourceResolver中的scan0函数里,使用String uriStr = removeTrailingSlash(uri.toString());获取uri字符串,但是如果路径中包含特殊字符,比如说空格,那么这种方法解析出来的字符串空格会变成%20,字符串的长度变了,导致后续scanFile里获取的类名有问题。

我将其修改为String uriStr = removeTrailingSlash(URLDecoder.decode(uri.toString(), StandardCharsets.UTF_8));后,发现可以规避这个现象。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.