搭建项目
然后点击下一步完成即可搭建成功
在pom文件中导入lombok的jar包,帮助我们快速创建实体类
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.16</version> </dependency>
创建实体类User和Cat
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;import java.util.List;@Data @AllArgsConstructor @NoArgsConstructor public class User {private Integer id;private String name;private String word;private Cat cat;private List<Cat> cats; }
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;@Data @AllArgsConstructor @NoArgsConstructor public class Cat {private Integer id;private String breed;private String name; }
传递非JSON格式的数据
主键:
@RequestParam 主要用于在Spring MVC后台控制层获取参数,它有三个常用参数
1). defaultValue 表示设置默认值
2). required 表示该参数是否必传
3). value 值表示接收传入的参数的key
@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出URL模板中的变量作为参数
1.前端请求参数的key需和后端控制层处理请求的方法参数名称一致
@RequestMapping("get1")public void get(String name,String word){System.out.println(name+":"+word);}
2.前端请求参数的key需和后端控制层处理请求的方法参数名称不一致
@RequestMapping("get2")public void get2(@RequestParam("n") String name,@RequestParam("w") String word){System.out.println(name+":"+word);}
3.前端请求参数的key和后端控制层处理请求方法的参数pojo实体类的属性名称一致
@RequestMapping("get3")public void get3(User user){System.out.println(user);}
4.使用@PathVariable注解将请求URL中的模板变量映射到功能 处理方法的参数上,如果模板变量名称和方法的参数名称不同需要在@PathVariable注解上显示的指定映射关系
@RequestMapping("get4/{name}/{word}")public void get4(@PathVariable("name") String name,@PathVariable("word") String word){System.out.println(name+""+word);}
5.通过HttpServletRequest对象获取数据,前端请求参数的key需和getParameter(String name)方法传递的参数名称一致
@RequestMapping("get5")public void get5(HttpServletRequest request){String id = request.getParameter("id");String name = request.getParameter("name");String word = request.getParameter("word");System.out.println(id+""+name+""+word);}
传递JSON格式的数据
如果前端通过application/json类型提交JSON格式的数据给后端控制层处理请求的方法,方法的参数必须使用@RequestBody注解进行修饰,才能接收来自前端提交的JSON数据
@RequestMapping("get6")public void get6(@RequestBody User user){System.out.println(user);}
1.单个实体接收参数
前端传递的json的数据:
{"id":1003,"name":"小元","word":"123" }
2. 实体嵌套实体接收参数
前端传递的json的数据:
{"id":1,"name":"张三","word":123,"cat":{"id":1001,"breed":"小白","name":"白白"} }
3. 实体嵌套List集合接收参数
前端传递的json的数据:
{"id":1,"name":"张三","word":123,"cat":{"id":1001,"breed":"小白","name":"白白"},"cats":[{"id":1002,"breed":"小黑","name":"黑黑"},{"id":1003,"breed":"小启","name":"启启"},{"id":1004,"breed":"小蓝","name":"蓝蓝"}] }
4. Map集合接收参数
@RequestMapping("get7")public void add10(@RequestBody Map<String,Object> m){//传递List类型的集合数据Integer id = (Integer) m.get("id");String name = (String) m.get("name");String word = (String) m.get("word");System.out.println(id+"\t"+name+":"+word);System.out.println("========");Map<String,Object> cat = (Map<String, Object>) m.get("cat");Set<String> s = cat.keySet();for (String s1 : s) {System.out.println(s1+":"+ cat.get(s1));}System.out.println("========");List< Map<String,Object>> course = (List<Map<String, Object>>) m.get("cats");for (Map<String, Object> cs : course) {Set<Map.Entry<String, Object>> e = cs.entrySet();for (Map.Entry<String, Object> ss : e) {System.out.println(ss.getKey()+":"+ss.getValue());}}}
前端传递的json的数据:
{"id":1,"name":"张三","word":"123","cat":{"id":1001,"breed":"小白","name":"白白"},"cats":[{"id":1002,"breed":"小黑","name":"黑黑"},{"id":1003,"breed":"小启","name":"启启"},{"id":1004,"breed":"小蓝","name":"蓝蓝"}] }