数据交互

144 阅读2分钟

「这是我参与2022首次更文挑战的第8天,活动详情查看:2022首次更文挑战」。

数据交互

在项⽬中,经常会遇到这样的场景,前端⻚⾯中发送ajax请求给Controller中的处理⽅法,处理⽅法返回 json格式的字符串给这个ajax请求。

当然,前⾯⻚⾯发送的请求中携带的参数,也可以是正常的k=v的形式

接收参数

1、参数是基本数据类型/包装类型/String

@RequestMapping("test") public String test(int age){...}

public String test(long id){...}

public String test(boolean flag){...}

public String test(Long id){...}

public String test(String name){...}

注意,参数名字要和客户端传的参数名⼀致,否则需要使⽤

@RequestParam 来指定参数名

2.参数是数组类型

@RequestMapping("test") public String test(int[] age){...}

public String test(long[] id){...}

public String test(boolean[] flag){...}

public String test(Long[] id){...}

public String test(String[] name){...}

注意,客户端传值类似于这样:name=tom&name=lisi

3、参数是数组类型(json格式)

@RequestMapping("/index")

@ResponseBody

public String index(@RequestBody String[] arr){...}

4、参数是类类型

例如,实体类Uesr、Student等

@RequestMapping("test")

public String test(User user){...}

注意,客户端传值类似于这样:

id=1&name=tom&dob=1999-11-11

5、参数是类类型的数组(json格式)

@RequestMapping("/test")

public String test(@RequestBody User[] users){...}

6、参数是List/Set集合(json格式)

@RequestMapping("/index")

@ResponseBody

public String index(@RequestBody List<String> list){...}

例如:

@RequestMapping("/index")

@ResponseBody

public String index(@RequestBody List<User> list){....}

7、参数是Map集合(json格式)

@RequestMapping("/index")

@ResponseBody

public String index(@RequestBody Map<Long,User> map){...}

8 采⽤restful⻛格进⾏请求:

@RestController

public class UserController {

@GetMapping("/hello/{userid}")

public String hello(@PathVariable("userid") String userid) {

System.out.println(userid);

return userid;

}

}

返回数据

1、给ajax请求返回单值

@ResponseBody

@RequestMapping(value="test1",consumes="application/json",produces="applic

ation/js

on" ,method=RequestMethod.POST)

public String test1(@RequestBody User user){

System.out.println("user = "+user);

return "hello world";

}

2、给ajax请求返回数组(json格式)

@ResponseBody

@RequestMapping(value="test2",consumes="application/json",produces="applicat

ion/js on" ,method=RequestMethod.POST)

public String[] test2(@RequestBody User user){

System.out.println("user = "+user);

return new String[]{"hello","world","tom"};

}

3、给ajax请求返回⾃定义类型的数组(json格式)

@ResponseBody

@RequestMapping(value="test3",consumes="application/json",produces="applicat

ion/js on" ,method=RequestMethod.POST)

public User[] test3(@RequestBody User user){

System.out.println("user = "+user);

return new User[]{new User(1L,"tom1"),new User(2L,"tom2"),new

User(3L,"tom3")};

}

4、给ajax请求返回List集合(json格式)

@ResponseBody

@RequestMapping(value="test4",consumes="application/json",produces="applicat

ion/js on" ,method=RequestMethod.POST)

public List<User> test4(@RequestBody User user){

System.out.println("user = "+user);

return Arrays.asList(new User(1L,"tom1"),new User(2L,"tom2"),new

User(3L,"tom3"));

}

5、给ajax请求返回Set集合(json格式)

@ResponseBody

@RequestMapping(value="test5",consumes="application/json",produces="applicat

ion/j son" ,method=RequestMethod.POST)

public Set<User> test5(@RequestBody User user){

System.out.println("user = "+user);

Set<User> set = new HashSet<>();

set.add(new User(1L,"tom1"));

set.add(new User(2L,"tom2"));

set.add(new User(3L,"tom3")); return set;

}

6、给ajax请求返回Map集合(json格式)

@ResponseBody

@RequestMapping(value="test6",consumes="application/json",produces="applicat

ion/j son" ,method=RequestMethod.POST)

public Map<String,User> test6(@RequestBody User user){

System.out.println("user = "+user);

Map<String,User> map = new HashMap<>();

map.put("tom1", new User(1L,"tom1"));

map.put("tom2", new User(2L,"tom2"));

map.put("tom3", new User(3L,"tom3"));

return map;

}