You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.2 KiB
80 lines
2.2 KiB
package cc.bnblogs.controller; |
|
|
|
import cc.bnblogs.entity.User; |
|
import cc.bnblogs.entity.UserList; |
|
import cc.bnblogs.entity.UserMap; |
|
import org.springframework.web.bind.annotation.*; |
|
|
|
import java.util.Arrays; |
|
|
|
/** |
|
* @description: SpringMVC数据绑定 |
|
* @author: zfp@bnblogs.cc |
|
* @date: 2023/3/25 15:05 |
|
*/ |
|
@RestController |
|
@RequestMapping("data") |
|
public class DataHandler { |
|
/** |
|
* 请求url: http://localhost:8080/data/id?id=10 |
|
* @param id |
|
* @return |
|
*/ |
|
@RequestMapping("id") |
|
public String getId(Integer id) { |
|
System.out.println(id); |
|
return id+""; |
|
} |
|
|
|
/** |
|
* 请求url: http://localhost:8080/data/name?names=aaa&names=bbb&names=cccc |
|
* @param names |
|
* @return |
|
*/ |
|
@RequestMapping("name") |
|
public String getNames(String[] names) { |
|
return Arrays.toString(names); |
|
} |
|
|
|
/** |
|
* 返回用户列表,SpringMVC不能直接接收一个List集合,只能将List集合封装为一个类 |
|
* @param userList 用户数据保存在列表中 |
|
* @return |
|
*/ |
|
@RequestMapping(value = "list",method = RequestMethod.POST) |
|
public String list(UserList userList) { |
|
System.out.println(userList); // 输出list中的数据 |
|
StringBuilder str = new StringBuilder(); |
|
for (User user : userList.getUsers()) { |
|
str.append(user); |
|
} |
|
return str.toString(); |
|
} |
|
|
|
/** |
|
* 返回用户数据,SpringMVC不能直接接收一个Map,只能将Map封装为一个类 |
|
* @param userMap 用户数据保存在Map的value中 |
|
* @return |
|
*/ |
|
@RequestMapping(value = "map",method = RequestMethod.POST) |
|
public String map(UserMap userMap) { |
|
// System.out.println(userMap); // 输出map中的数据 |
|
StringBuilder str = new StringBuilder(); |
|
for (String key : userMap.getUsers().keySet()) { |
|
User user = userMap.getUsers().get(key); |
|
str.append(user); |
|
} |
|
return str.toString(); |
|
} |
|
|
|
/** |
|
* 接收一个json数据,返回User对象 |
|
*/ |
|
@RequestMapping(value = "json",method = RequestMethod.POST) |
|
public User getJson(@RequestBody User user) { |
|
System.out.println("user = " + user); |
|
return user; |
|
} |
|
|
|
|
|
}
|
|
|