Spring MVC数据绑定入门总结

1.基本类型

基本类型参数不可为空
正例:http://localhost:8080/demo/he?id=2
反例:http://localhost:8080/demo/he?id=(报400错误)
注意:String不是基本类型,具体参考>Java数据类型

@RestController //定义控制器返回字符串
public class HelloController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printHello(int id) {
        return "Response:"+id;
    }

    //添加@RequestParam(required=false)参数可为空
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printHello(@RequestParam(required=false)int id) {
        return "Response:"+id;
    }

}

2.包装类和数组的处理

@RestController 
public class HelloController {

    // http://localhost:8080/demo/hello?id=1
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printHello(Integer id) {
        return "Response:"+id;
    }

    // http://localhost:8080/demo/hi?name=Tom&name=Jim
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String printHello(String[] name) {
        return "Response:"+anme;
    }

}

3.简单对象&多层级对象&同属性对象

    //简单对象
    //http://127.0.0.1:8080/Spring/hi?age=10&name=Tom
    @RequestMapping(value="/hi", method = RequestMethod.GET)    
    @ResponseBody
    public String object(Student student){
        return student.toString();
    }

    //多层级对象
    //http://127.0.0.1:8080/Spring/hi2?age=10
    //&name=Tom&student.age=18
    @RequestMapping(value="/hi2", method = RequestMethod.GET)   
    @ResponseBody
    public String object2(Admin admin){
        return admin.toString();
    }

    //同属性的多对象
    //http://127.0.0.1:8080/Spring/hi3?
    //name=Tom&student.age=18
    @RequestMapping(value="/hi3", method = RequestMethod.GET)   
    @ResponseBody
    public String object3(Student student, Admin admin){
        return student.toString()+admin.toString();
    }

    //同属性的多对象赋值 需指定对象的前缀
    @InitBinder("student")
    public void initStudent(WebDataBinder binder){
        binder.setFieldDefaultPrefix("student.");
    }

    //同属性的多对象赋值 需指定对象的前缀
    @InitBinder("admin")
    public void initStudent2(WebDataBinder binder){
        binder.setFieldDefaultPrefix("admin.");
    }

4.List&Set&Map

public class ListFrom {
    private List<Student> users ;

    getter setter...

    @Override
    public String toString() {
        return "ListFrom [users=" + users + "]";
    }

}
public class MapFrom {
    private Map<String, Student> users ;

    getter setter...

    @Override
    public String toString() {
        return "MapFrom [users=" + users + "]";
    }   

}
@RestController
public class DataController {

    //List对象
    //http://127.0.0.1:8080/Spring/list?
    //users[0].age=10&users[1].name=Tom
    @RequestMapping(value="/list", method = RequestMethod.GET, produces={"text/html;charset=UTF-8"})    //支持中文
    public String object2(ListFrom s){
        return s.toString();
    }

    //Map对象
    //http://127.0.0.1:8080/Spring/map?
    //users["X"].age=10&users["Y"].name=Tom
    @RequestMapping(value="/map", method = RequestMethod.GET)   
    public String object3(MapFrom s){
        return s.toString();
    }
}

5.Json或Xml 数据参数

@RequestBody 注解
    //发送json数据到该url即可实现数据绑定
    @RequestMapping(value="/json", method = RequestMethod.POST) 
    public String object3(@RequestBody Student s){
        return s.toString();
    }

对于xml数据,需要在实体类中添加注释
这里写图片描述
然后

    //发送xml数据到该url即可实现数据绑定
    @RequestMapping(value="/xml", method = RequestMethod.POST)  
    public String object3(@RequestBody Admin s){
        return s.toString();
    }

6. PropertyEditor、Formatter、Converter

局部配置

    //PropertyEditor
    //http://127.0.0.1:8080/Spring_iMooc/date1?date1=2018-01-01
    @RequestMapping(value="/date1", method = RequestMethod.GET) 
    public String date1(Date date1){
        return date1.toString();
    }
    @InitBinder("date1")
    public void initDate1(WebDataBinder binder){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

全局配置
1)自定义Formatter
这里写图片描述
2)注入
这里写图片描述
3)使用
这里写图片描述
也可以使用自定义Converter
1)自定义
这里写图片描述
2)配置
这里写图片描述
3)使用

7.RESTful扩展

简单来说,就是把参数移到URL上,比如下面。

   https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/pdf/spring-boot-reference.pdf
  http://jbk.39.net/xebm/

使用方法

@RequestMapping("/subject/{name}")
public String demo(@PathVariable("name") String name){
  return name;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值