基本类型、包装类型的数据绑定以及坑

来源:互联网 发布:2016淘宝销量排行榜 编辑:程序博客网 时间:2024/06/05 18:19

基本类型、包装类型的数据绑定以及坑

例如,age

可以是基本类型:int、也可以是包装类型:Integer


情景一:age为int类型时

错误:参数不是int类型时,springmvc报错400



错误:不传参数,springmvc报错500












情景二:age为包装类型。例如Integer

当然我们实际开发过程中,也可以通过

@RequestParam()
@RequestParam()注解对是否进行配置



下面给出我的实例代码:

package com.wsq.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;/** * Created by wsq on 2017/12/17下午9:31. * 本章主要介绍基本类型、包装类型的数据绑定以及坑 */@Controllerpublic class TestController {    //TODO http://localhost:8080/baseType.do?xage=10    @RequestMapping(value = "baseType.do", method = RequestMethod.GET)    @ResponseBody    public String baseType(@RequestParam("xage") int age){        return "age: " + age;    }    //TODO http://localhost:8080/baseType2.do?age=10    @RequestMapping(value = "baseType2.do", method = RequestMethod.GET)    @ResponseBody    public String baseType2(Integer age){        return "age: " + age;    }    //TODO http://localhost:8080/array.do?name=Tom&name=Lucy&name=Jim    @RequestMapping(value = "array.do", method = RequestMethod.GET)    @ResponseBody    public String baseType2(String[] name){        StringBuilder sbf = new StringBuilder();        for (String item : name) {            sbf.append(item).append(" ");        }        return sbf.toString();    }}