SpringMVC注解、@Controller注解和@RestController注解的区别、@RequestMapper、@PathVariable
DAY28.1 Java核心基础
单体架构
SpringMVC注解
Spring MVC 提供了强大的注解机制,大大简化代码的开发,提升程序的可扩展性
@RequestMapper
通过该注解将URL与业务代码进行映射联系
同时也可以映射到控制器,相当于多一个访问路径
@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;@RequestMapping(value = "/test",params = "test")public String test(){return "test";}@RequestMapping(value = "/list",method = RequestMethod.GET)public List<User> list(){return userService.list();}}
访问路径:localhost:8080/user/test
参数
value:指定URL请求的路径,@RequestMapper的默认参数
@RequestMapping(“/test”) 等于 @RequestMapping(value=“/test”)
method:指定访问的请求方式,POST,GET…
@RequestMapping(value = "/test",method = RequestMethod.GET)
等于
@GetMapping("/test")
指定请求的方法为GET
可以看见get请求可以访问,但是post请求报错405
@RequestMapping(value=“/index”,method=RequestMethod.GET) = @GetMapping(“/index”)
@RequestMapping(value=“/index”,method=RequestMethod.PUT) = @PutMapping(“/index”)
@RequestMapping(value=“/index”,method=RequestMethod.DELETE) = @DeleteMapping(“/index”)
params:指定请求必须存在的参数,如果没有则无法访问
@RequestMapping(value = "/test",params = "name",method = RequestMethod.GET)
指定/user/test请求必须存在name参数,比如请求为http://localhost:8080/user/test?name=abc
常见的请求错误
- 404:请求找不到,注意路径是否错误
- 405:请求方式有问题,注意查看请求的指定方法比如GET、POST…
- 400:参数不匹配,注意检查一下参数是否正确
- 500:后台抛出异常,检查后端逻辑
传递参数
传统的传递参数
/user/test?name=…
@PathVariable
更多的使用RestFul的传参方式(路径传递参数)
RESTful:http://localhost:8080/user/list/abc/10
@RequestMapping(value = "/list/{name}/{id}")
@ResponseBody
public List<User> list(@PathVariable("name") String title,@PathVariable("id") Integer num){System.out.println(title);System.out.println(num);return this.userService.list();
}
用@PathVariable标注映射路径的参数
对象接受的参数
@PostMapping("/test2")
public User test2(User user){return user;
}
可以访问http://localhost:8080/user/test2?name=“张三”
只需要参数与实体类的字段匹配即可
@Controller注解和@RestController注解的区别
@Controller
- 属于 Spring MVC 的标准注解,用于标识一个控制器类。
- 默认返回的是视图(View)名称,一般用于服务端渲染 HTML 页面。
- 如果需要返回 JSON 或 XML 数据,需在方法上添加
@ResponseBody
注解。
示例:
@Controller
public class HelloController {@RequestMapping("/hello")@ResponseBodypublic String hello() {return "Hello, World!";}@RequestMapping("/page")public String page() {return "index"; // 返回视图名,会由视图解析器解析为 /WEB-INF/views/index.jsp}
}
@RestController
- 是
@Controller
和@ResponseBody
的组合注解,用于构建 RESTful API。 - 所有方法默认都以 JSON 或 XML 格式直接返回对象数据,而不是视图。
- 常用于前后端分离的项目。
示例:
@RestController
public class ApiController {@RequestMapping("/api/hello")public String hello() {return "Hello, REST!";}@RequestMapping("/api/user")public User getUser() {return new User("张三", 18);}
}
Spring MVC 数据绑定
数据绑定:后台方法直接获取 HTTP 请求的参数
底层:HTTP 请求传输的参数都是String类型,业务方法中的参数都是指定的类型,所以需要类型转换,SpringMVC会自动转换
String 转换为需要的类型
基本数据类型
@GetMapping("byBaseType")
public String byBaseType(int id){return "id:"+id;
}
访问的时候如果没传递参数id,导致id为空则会报错
比如localhost:8080/user/byBaseType
但是如果改为Integer包装类则不会报错
@GetMapping("byBaseType")
public String byBaseType(Integer id){return "id:"+id;
}
因为基本数据类型不能为null
而包装类为引用类型可以为null
传递数组
@GetMapping("/arrayType")
public String arrayType(String[] names){StringBuilder sb = new StringBuilder();for (String name : names) {sb.append(name).append(",");}return sb.toString();
}
访问传递数组的参数:http://localhost:8080/user/arrayType?names=abc&names=bcd&names=shuwu
级联对象
@Data
public class Student {private Integer id;private String name;private Integer age;private Address address;
}
@Data
public class Address {private Integer id;private String city;
}
@GetMapping("/student")
public Student student(Student student){return student;
}
如何传递address字段呢,通过address.id=…来赋值
http://localhost:8080/user/student?id=1&name=shuwu&age=18&address.id=1&address.city=chongqing