@RequestMapping(““)括号里面的内容的作用

今天学习springboot时访问controller类时出现了This application has no explicit mapping for /error, so you are seeing this as a fallback。错误,原因是我没有写后缀localhost:8080/hello中的hello这个hello是在@RequestMapping("/hello")的括号里,如果不写的就定位不到这个类,这样的话就会出现错位。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@RestController是Spring框架中的一个注解,用于标识一个类是一个RESTful风格的控制器。它的作用是将该类中的方法返回的数据直接以JSON或XML的形式写入HTTP响应中,而不是返回一个视图。这样可以方便地开发基于RESTful风格的Web服务。 @RequestMapping是Spring框架中的一个注解,用于映射URL和方法之间的关系。它可以用在类级别和方法级别上。在类级别上,它可以定义一个基本的URL路径,而在方法级别上,它可以定义具体的URL路径和HTTP请求方法。 @RestController和@RequestMapping通常一起使用。当一个类被@RestController注解标识时,它的所有方法都会默认被@RequestMapping注解标识。这样,我们可以通过在方法上使用@RequestMapping注解来定义具体的URL路径和HTTP请求方法,从而实现对不同URL的请求进行处理。 例如,下面是一个使用@RestController和@RequestMapping的示例: ```java @RestController @RequestMapping("/api") public class UserController { @RequestMapping(value = "/users", method = RequestMethod.GET) public List<User> getAllUsers() { // 返回所有用户的列表 } @RequestMapping(value = "/users/{id}", method = RequestMethod.GET) public User getUserById(@PathVariable int id) { // 根据用户ID返回用户信息 } @RequestMapping(value = "/users", method = RequestMethod.POST) public void createUser(@RequestBody User user) { // 创建新用户 } @RequestMapping(value = "/users/{id}", method = RequestMethod.PUT) public void updateUser(@PathVariable int id, @RequestBody User user) { // 更新用户信息 } @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE) public void deleteUser(@PathVariable int id) { // 删除用户 } } ``` 在上面的示例中,@RestController注解标识了UserController类是一个RESTful风格的控制器,@RequestMapping注解定义了基本的URL路径为"/api"。然后,每个方法上的@RequestMapping注解定义了具体的URL路径和HTTP请求方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值