Spring:MediaType、MultiPartFile、@InitBinder

本文介绍了MediaType在决定浏览器解析资源格式中的作用,Spring的MediaType类以及如何使用MultiPartFile接口进行文件操作,重点讲解了MockMultipartFile在测试中的应用。此外,还详细阐述了@InitBinder注解在数据绑定中的使用,特别是与日期格式转换相关的内容。
摘要由CSDN通过智能技术生成

一、MediaType媒体类型(对象)

决定浏览器以什么形式、什么编码对资源进行解析,一般用来指定请求头中Content-Type中的内容。

1.MediaType类型

类型描述
text/htmlhtml格式
text/xmlxml格式
text/x-markdownMarkdown格式
text/plain纯文本,空格转换为+,不对特殊字符进行编码
image/jpegjpeg图片格式
image/pngpng图片格式
image/gifgif图片格式
application/xhtml-xmlXHTML格式
application/xmlxml数据格式
application/json序列化后的json类型
application/pdf

PDF类型

application/x-www-form-urlencoded表单默认在发送前的编码类型,参数值是键值对,如果不在表单元素<form> 的enctype属性中规定编码格式,则默认为此种类型。
application/msword

Word类型

application/octet-streamoctet(八位位组);二进制数据流,用于文件下载上传。
multipart/form-data表单在发送前可选编码类型,urlencoded编码效率较低,需要对字符进行编码。该类型就是不对表单内容进行编码,直接发送大量二进制数据或者non-ASCII码,需要在form表单的enctype属性中指定。

2.Spring的MediaType类

  • 提供了所有媒体类型的不可更改的静态属性,用全大写来表示。
  • parseMediaType(String MediaType)传入字符串转换为MediaType对象。

二、MultiPartFile接口及其实现类

1.MultiPartFile

1.1MultiPartFile接口继承了InputStreamSource,因此拥有getInputStream方法,返回InputStream(输入流对象)

1.2规定了诸如getName、getOriginalFileName、getContentType、isEmpty、transferTo等方法方便我们操作。

2.三个实现类

        MockMultipartFile

2.1主要用于在单元测试中,模拟文件上传和下载,方便对文件上传和下载功能进行测试。

MockHttpServletRequest myRequest = new MockHttpServletRequest();
        myRequest.setContentType("multipart/form-data");
        MockMultipartHttpServletRequest myMultipartHttpServletRequest = new MockMultipartHttpServletRequest(myRequest.getServletContext());
        //上传文件
        MockMultipartFile myMockFile = new MockMultipartFile("oneDream","oneDream.txt","text/plain","I have a dream,this is to be better!".getBytes());
        myMultipartHttpServletRequest.addFile(myMockFile);
        //获取文件
        MultipartFile oneDream = myMultipartHttpServletRequest.getFile("oneDream");
        String originalFilename = oneDream.getOriginalFilename();
        System.out.println(originalFilename);

        CommonsMultipartFile和StandardMultipartFile

2.2CommonsMultipart主要依赖的包是Apache Commons FileUpload库中一个类和Spring框架StandardMultipartFile直接依赖于原生Servlet API,不需要额外的第三方库,可以根据不同情况采用不同的对象来实现文件的上传下载。

三、@InitBinder注解

1.使用场景

被该注解标注后,下面的方法会被用于数据绑定,在Spring自动绑定的过程中,可以根据默认的变量名一致原则自动进行参数和对象属性绑定,但是对于一些特殊类型(自己声明的类、Date)无法自动绑定,这时就需要用到@InitBinder。

2.WebDataBinder

该对象用来给指定实例的属性绑定值,需要在参数处传入PropertyValues类型的参数。

2.1MutablePropertyValues

是PropertyValues的实现类,该类当中的可以存入PropertyValues类型的数据(键值对)当中的add方法能够添加属性键值对。使用例子如下:

 Person person = new Person();
        //数据绑定
        WebDataBinder wdb = new WebDataBinder(person,"person");
        MutablePropertyValues mpv = new MutablePropertyValues();
        mpv.add("name","小李飞刀");
        mpv.add("_age",33);
        mpv.add("age",22);
        wdb.bind(mpv);
        System.out.println(person);
//这样person对象中各个属性就被添加上了值
3.使用方法
@ControllerAdvice
public class BinderClass {
    @InitBinder  //该注解注解的方法参数只能是DataBinder或其实现类
    public void initBinder(WebDataBinder webDataBinder){
        //对日期格式进行规定
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setLenient(true);//自动计算日期
        //注册CustomEditor对象
        webDataBinder.registerCustomEditor(Data.class,new CustomDateEditor(sdf,true));
    }
}

@ControllerAdvice注解的类中,使用@InitBinder注解的方法中webDataBinder添加的绑定规则,会添加到全局,如果@InitBinder注解的方法直接写在Controller中,则仅仅对该Controller生效。

class Me{
        String name;
        Date birth;
        //这里省略了基本结构
    }
    //下面是学习过程中用于测试的映射

     @ResponseBody
    @GetMapping("/test/bind")
    public Me testWebDataBinder(@RequestParam String name,@RequestParam Date birth){
        Me me = new Me(name,birth);
        return me;
    }

我们再在Controller中,加上上面的请求映射,我们会接收来自请求的路径参数(Param),参数中的日期是字符串类型,会根据我们@InitBinder注解中添加的转换规则,自动转换为Date类型!

这里特别注意!虽然在SimpleDateFormat中我们规定了日期格式为"yyyy-MM-dd HH:mm:ss",但在路径传参过程中切勿如此表示:例子:"?name=大炮&birth=2001-11-12 15:34:23",应该使用"?name=小王&birth=2001/11/12 15:34:23"。/代替-。原因为何暂不得而知。


请求测试后,得到结果:

{"name":"小王","birth":"2001-11-12T04:00:01.000+00:00"}

  • 19
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值