SpringMVC基础配置
上一节已经将SpringMVC的框架搭好了,就不再多说
接收参数
url中的参数
请求路径为http://127.0.0.1/user/test?name=john&age=10
1 2 3 4 5 6 7 8 9
| @RequestMapping("/user/test")
@ResponseBody
public String save(@RequestParam(value = "name") String name,@RequestParam(value = "age") Integer age) { System.out.println("name:" + name + ",age:" + age); return "/user/test"; }
|
表单中的参数
与url中的参数的访问方式一样
1 2 3 4 5 6 7 8 9
| @RequestMapping("/user/test")
@ResponseBody
public String save(@RequestParam(value = "name") String name,@RequestParam(value = "age") Integer age) { System.out.println("name:" + name + ",age:" + age); return "/user/test"; }
|
数组
请求路径为http://127.0.0.1/user/test?likes=game&likes=tv&likes=reading
1 2 3 4 5 6 7 8 9 10
| @RequestMapping("/user/test")
@ResponseBody
public String save(@RequestParam(value = "likes") List<String> likes) { System.out.println(likes); return "/user/test"; }
|
用对象的属性接收
使用对象的属性接收,属性变量名必须与参数名相同,否则不会识别
请求路径为http://127.0.0.1/user/test?name=john&age=20&balance=100&gender=male&address.province=sichuan&address.city=chengdu
1 2 3 4 5 6 7 8 9 10
| @RequestMapping("/user/test")
@ResponseBody
public String save(User user) { System.out.println(user); return "/user/test"; }
|
打印如下
1
| User{id=null, name='john', gender='male', age=20, phone='null', birthday=null, balance=100.0, address=Address{province='sichuan', city='chengdu'}}
|
接收时间类型的参数
请求路径为http://127.0.0.1/user/test?date=2001/06/24
1 2 3 4 5 6 7 8 9
| @RequestMapping("/user/test")
@ResponseBody
public String save(@DateTimeFormat(pattern = "yyyy/MM/dd") Date date) { System.out.println(date); return "/user/test"; }
|
打印如下
1
| Sun Jun 24 00:00:00 CST 2001
|
全局配置
配置json数据的读取
导入jackson坐标用于json处理
1 2 3 4 5 6
| <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency>
|
在org.example.config.SpringMvcConfig中开启功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package org.example.config;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan({"org.example.controller"})
@EnableWebMvc public class SpringMvcConfig { }
|
设置json格式的请求,在json格式的请求体中写入以下信息
1 2 3 4 5 6 7 8 9 10 11 12
| { "name":"马昆", "gender":"male", "age": 21, "phone":"19960798888", "birthday":"2001-06-24", "balance":100.00, "address": { "province":"四川", "city":"眉山" } }
|
控制器接收
1 2 3 4 5 6 7 8 9
| @RequestMapping("/user/test")
@ResponseBody
public String save(@RequestBody User user) { System.out.println(user); return "/user/test"; }
|
json格式数据的发送
创建一个专属的消息类org.example.domain.JsonResult
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| package org.example.domain;
public class JsonResult { private Integer code; private String message; private Object data;
public JsonResult() { }
public JsonResult(Integer code, String message, Object data) { this.code = code; this.message = message; this.data = data; }
@Override public String toString() { return "JsonResult{" + "code=" + code + ", message='" + message + '\'' + ", data=" + data + '}'; }
public Integer getCode() { return code; }
public void setCode(Integer code) { this.code = code; }
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
public Object getData() { return data; }
public void setData(Object data) { this.data = data; } }
|
更改控制器方法的返回值
1 2 3 4 5 6 7 8 9 10
| @RequestMapping("/user/test")
@ResponseBody
public JsonResult save(@RequestBody User user) { return new JsonResult(200, "配置成功!", user); }
|
如果请求的格式正确,出现返回以下结果,时间格式传递的是一个时间戳
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| { "code": 200, "message": "配置成功!", "data": { "id": null, "name": "马昆", "gender": "male", "age": 21, "phone": "19960798888", "birthday": 993340800000, "balance": 100.0, "address": { "province": "四川", "city": "眉山" } } }
|
解析中文
在请求体中增加中文字段,出现以下情况
1 2 3 4 5
| { "code": 200, "message": "配置成功!", "data": "ä¸æ" }
|
在org.example.config.ServletControllerInitConfig中添加过滤器
1 2 3 4 5 6 7 8 9
| @Override protected Filter[] getServletFilters() { CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter(); encodingFilter.setEncoding("UTF-8"); return new Filter[]{encodingFilter}; }
|
重启服务再次访问,数据获取正常
1 2 3 4 5
| { "code": 200, "message": "配置成功!", "data": "中文" }
|