1.为阅读体验,本站无任何广告,也无任何盈利方法,站长一直在用爱发电,现濒临倒闭,希望有能力的同学能帮忙分担服务器成本
2.捐助10元及以上同学,可添加站长微信lurenzhang888,备注捐助,网站倒闭后可联系站长领取本站pdf内容
3.若网站能存活下来,后续将会持续更新内容
Web相关注解有@Controller、@RestController、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@ResponseBody、@RequestBody、@PathVariable
@Controller注解有什么用?
@Controller,对应 Spring MVC控制层,主要用户接受用户请求并调用 Service 层返回数据给前端页面。
@ResponseBody注解有什么用?
@ResponseBody通常和@Controller一起使用,@ResponseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区(响应体中),通常用来返回JSON数据或者是XML。
@RequestMapping("/login")
@ResponseBody
public User login(User user){
return user;
}
@RestController注解有什么用?
@RestController注解的作用就相当于@Controller加上@ResponseBody。
@RequestMapping注解有什么用?
@RequestMapping 用来标识http请求地址与Controller类的方法之间的映射。
@RequestMapping标识一个类:设置映射请求的请求路径的初始信息。
@RequestMapping标识一个方法:设置映射请求的请求路径的具体信息。
注意:如果类上和方法上都有这个注解,那么就先访问初始信息,然后再访问具体信息,举例如下
@Controller
@RequestMapping(value="/lurenzhang")
public class HelloWorld {
@RequestMapping("/helloworld")
public String hello(){
return "hello,world!";
}
}
上述代码的url映射地址为,你的域名+/lurenzhang/helloworld。
@PathVariable 注解有什么用?
@PathVariable 映射 URL 绑定的占位符,通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中,很容易理解,如下
@RequestMapping(value="/getUserById/{name}", method = RequestMethod.GET)
public User getUser(@PathVariable("name") String name){
return userService.selectUser(name);
}
将url中的name传到方法中的name
@RequestBody注解有什么用?
注解@RequestBody接收的参数是来自requestBody中,即请求体。一般用于处理非 Content-Type: application/x-www-form-urlencoded
编码格式的数据,比如:application/json
、application/xml
等类型的数据。而 Content-Type: application/x-www-form-urlencoded
编码格式的数据通常用@RequestParam注解。如下
@Controller
public class UserController {
@RequestMapping(value="/lurenzhang",method = RequestMethod.POST)
public void getUser(@RequestBody User user){
System.out.println(user);
}
}
@GetMapping、@PostMapping、@PutMapping、@DeleteMapping注解有什么用?
这几个注解一看就是对应着http请求中的get、post、put、delete方法,
@Controller
@RequestMapping(value="/lurenzhang")
public class HelloWorld {
@GetMapping("/helloworld")
public String hello(){
return "hello,world!";
}
}
上述代码的url映射地址为,你的域名+/lurenzhang/helloworld,等价于
@Controller
@RequestMapping(value="/lurenzhang")
public class HelloWorld {
@RequestMapping("/helloworld", method=RequestMethod.GET)
public String hello(){
return "hello,world!";
}
}
其他几个也都是类的,不再举例
本站链接:https://www.mianshi.online,如需勘误或投稿,请联系微信:lurenzhang888
点击面试手册,获取本站面试手册PDF完整版