美文网首页
Spring in action 阅读笔记3

Spring in action 阅读笔记3

作者: 言西枣 | 来源:发表于2017-06-08 17:35 被阅读20次
    5.3接受请求的输入

    @RequestParam(value="", defaultValue="") long max
    尽管defaultValue是String类型的值,但是绑定到max参数时,会转换成Long类型

    "/spittles/12345" vs "/spittles/show?spittle_id=12345"
    前者能够识别出要查询的资源,后者描述的是带有参数的一个操作-本质上是通过HTTP发起的RPC

    7.1配置的替代方案

    添加其他的Servlet与Filter
    实现WebApplicationInitializer,注册其他组件

    7.2Multipart
    1. MultipartResolver
      A. StandardServletMultipartResolver 配置信息在DispatcherServlet之中
    @Bean
    public MultipartResolver multipartResolver() throws IOException {
    return new StandardServletMultipartResolver();
    }
    
    // 1.WebApplicationInitializer
    DispatcherServlet ds = new DispatcherServlet();
    Dynamic registration = context.addServlet("appServlet", ds);
    registration.addMapping("/");
    registration.setMultipartConfig(
    new MultipartConfigElement("/tmp/spittr/uploads"));
    //
    // 2.AbstractAnnotationConfigDispatcherServletInitializer
    @Override
    protected void customizeRegistration(Dynamic registration) {
    registration.setMultipartConfig(
    new MultipartConfigElement("/tmp/spittr/uploads"));
    }
    //
    // 3.web.xml
    <servlet>
        <servlet-name>SpringDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <multipart-config>
            <location>/tmp</location>
            <max-file-size>5242880</max-file-size><!--5MB-->
            <max-request-size>20971520</max-request-size><!--20MB-->
            <file-size-threshold>0</file-size-threshold>
        </multipart-config>
    </servlet>
    

    B. CommonsMultipartResolver
    只需要配置bean

    @Bean
    public MultipartResolver multipartResolver() throws IOException {
    CommonsMultipartResolver multipartResolver =
    new CommonsMultipartResolver();
    multipartResolver.setUploadTempDir(
    new FileSystemResource("/tmp/spittr/uploads"));
    multipartResolver.setMaxUploadSize(2097152);
    multipartResolver.setMaxInMemorySize(0);
    return multipartResolver;
    }
    

    MultipartResolver在DispatcherServlet转发请求之前对request进行处理,如果是Multipart的请求,就分解为MultipartHttpServletRequest

    1. 请求
      POST enctype="multipart/form-data"
      <input type="file" name="" accept="" />
    2. 处理Multipart请求
      A. @RequestPart("name") byte[] filebyte
      B. @RequestPart("name") MultipartFile/Part file
      [Facepalm][Facepalm]如果使用Part参数接收文件,就不需要配置MultipartResolver了
    7.3异常处理
    • 使用@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="Not Found")标注异常,将异常映射到状态码
    • 使用@ExceptionHandler(Exception.class)标注异常处理方法,处理同一个Controller中handle method(RequestMapping)抛出的此类异常
    • 使用@ControllerAdvice进行全局的异常处理,处理所有Controller抛出的此类异常
    • Any Spring bean declared in the DispatcherServlet’s application context that implements HandlerExceptionResolver will be used to intercept and process any exception raised in the MVC system and not handled by a Controller. The interface looks like this:

    public interface HandlerExceptionResolver {
    
        /**
         * Try to resolve the given exception that got thrown during on handler execution,
         * returning a ModelAndView that represents a specific error page if appropriate.
         * <p>The returned ModelAndView may be {@linkplain ModelAndView#isEmpty() empty}
         * to indicate that the exception has been resolved successfully but that no view
         * should be rendered, for instance by setting a status code.
         * @param request current HTTP request
         * @param response current HTTP response
         * @param handler the executed handler, or {@code null} if none chosen at the
         * time of the exception (for example, if multipart resolution failed)
         * @param ex the exception that got thrown during handler execution
         * @return a corresponding ModelAndView to forward to,
         * or {@code null} for default processing
         */
        ModelAndView resolveException(
                HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);
    
    }
    
    

    相关文章

      网友评论

          本文标题:Spring in action 阅读笔记3

          本文链接:https://www.haomeiwen.com/subject/ubexfxtx.html