文章目录
1.设置请求映射路径
- 导入依赖 jar 包
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
</dependencies>
- 创建 SpringMvcConfig 配置类
@Configuration
@ComponentScan("com.my.controller")
public class SpringMvcConfig {
}
- 创建 Servlet 初始化配置类
public class ServletContextInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
//中文乱码处理
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter=new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
- 设置映射路径
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/delete")
@ResponseBody
public String delete(){
System.out.println("user delete......");
return "{'msg':'user delete'}";
}
}
2.接收请求参数
- 数据模型类
public class User {
private String name;
private int age;
private Address address;
······(get和set)
}
public class Address {
private String city;
private String pro;
······(get和set)
}
- 控制类接收参数
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(String name,int age){
System.out.println("user save name ===>"+name);
System.out.println("user save age ===>"+age);
return "{'msg':'user save'}";
}
}
3.五种类型参数传递
- 使用的是Postman工具
1.普通参数传递(前端和后台的名称要一致)
@RequestMapping("/save")
@ResponseBody
public String save(String name,int age){
System.out.println("user save name ===>"+name);
System.out.println("user save age ===>"+age);
return "{'msg':'user save'}";
}
2.普通Java对象参数传递
@RequestMapping("/pojo")
@ResponseBody
public String pojo(User user){
System.out.println("user pojo user ===>"+user);
return "{'msg':'user pojo'}";
}
3.嵌套Java对象参数传递
- 两个实体类
public class User {
private String name;
private int age;
private Address address;
······(get和set)
}
------------------------------------------------
public class Address {
private String city;
private String pro;
······(get和set)
}
2.接收参数
@RequestMapping("/pojo")
@ResponseBody
public String pojo(User user){
System.out.println("user pojo user ===>"+user);
return "{'msg':'user pojo'}";
}
4.数组类型参数传递
@RequestMapping("/array")
@ResponseBody
public String array(String[] likes){
System.out.println("user array likes ===>"+ Arrays.toString(likes));
return "{'msg':'user array'}";
}
5.集合类型参数传递
- RequestParam 绑定请求参数与处理器方法形参之间的关系
@RequestMapping("/list")
@ResponseBody
public String list(@RequestParam List<String> likes){
System.out.println("user list likes ===>"+ likes);
return "{'msg':'user list'}";
}
4. JSON数据传输参数
- 导入依赖 jar 包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
- 在 SpringMvcConfig 配置类上,开启 JSON 数据类型自动转换
@Configuration
@ComponentScan("com.my.controller")
//开启json
@EnableWebMvc
public class SpringMvcConfig {
}
1. JSON普通数组传递参数
@RequestMapping("/listJson")
@ResponseBody
public String listJson(@RequestBody List<String> likes){
System.out.println("user list likes ===>"+ likes);
return "{'msg':'user list'}";
}
2. JSON对象数据传递参数
@RequestMapping("/pojoJson")
@ResponseBody
public String pojoJson(@RequestBody User user){
System.out.println("user pojo user ===>"+user);
return "{'msg':'user pojo'}";
}
3. JSON对象数组传递参数
@RequestMapping("/pojoListJson")
@ResponseBody
public String pojoListJson(@RequestBody List<User> users){
System.out.println("user list likes ===>"+ users);
return "{'msg':'user list'}";
}
5.日期类型参数传递
- 三种格式
- 2022/11/13
- 2022-11-12
- 2022-11-12 19:13:10
@RequestMapping("/date")
@ResponseBody
public String date(Date date1,
@DateTimeFormat(pattern = "yyyy-MM-dd") Date date2,
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date3){
System.out.println("book date1......"+date1);
System.out.println("book date2......"+date2);
System.out.println("book date3......"+date3);
return "{'msg':'book date'}";
}
6.响应 JSON 数据
- 实体类
public class Book {
private Integer id;
private String type;
private String name;
private String description;
······(get和set方法)
}
@RequestMapping("/getAll")
@ResponseBody
public List<Book> getAll(){
System.out.println("book getAll running......");
List<Book> bookList=new ArrayList<>();
Book book1=new Book();
book1.setType("计算机");
book1.setName("SpringMVC入门教程");
book1.setDescription("小试牛刀");
bookList.add(book1);
Book book2=new Book();
book2.setType("计算机");
book2.setName("SpringMVC实战教程");
book2.setDescription("一代宗师");
bookList.add(book2);
return bookList;
}