Java中Spring MVC常用的注解有哪些

Spring MVC提供了许多注解来简化和规范Web应用程序的开发。以下是一些常用的Spring MVC注解及其用途:

控制器相关注解

  1. @Controller

    • 标识一个类为Spring MVC控制器,用于处理HTTP请求。
    import org.springframework.stereotype.Controller;
    
    @Controller
    public class MyController {
        // ...
    }
    
  2. @RestController

    • 标识一个类为RESTful Web服务的控制器,相当于@Controller@ResponseBody的结合。
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyRestController {
        // ...
    }
    

请求映射相关注解

  1. @RequestMapping

    • 用于映射请求URL到处理方法或类,可以用于类级别和方法级别。
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/api")
    public class ApiController {
        
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String sayHello() {
            return "Hello, World!";
        }
    }
    
  2. @GetMapping

    • 专用于处理HTTP GET请求,是@RequestMapping(method = RequestMethod.GET)的快捷方式。
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
        
        @GetMapping("/hello")
        public String sayHello() {
            return "Hello, World!";
        }
    }
    
  3. @PostMapping

    • 专用于处理HTTP POST请求,是@RequestMapping(method = RequestMethod.POST)的快捷方式。
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
        
        @PostMapping("/submit")
        public String handleSubmit() {
            return "Form Submitted!";
        }
    }
    
  4. @PutMapping

    • 专用于处理HTTP PUT请求,是@RequestMapping(method = RequestMethod.PUT)的快捷方式。
    import org.springframework.web.bind.annotation.PutMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
        
        @PutMapping("/update")
        public String handleUpdate() {
            return "Updated!";
        }
    }
    
  5. @DeleteMapping

    • 专用于处理HTTP DELETE请求,是@RequestMapping(method = RequestMethod.DELETE)的快捷方式。
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
        
        @DeleteMapping("/delete")
        public String handleDelete() {
            return "Deleted!";
        }
    }
    

请求参数和响应处理相关注解

  1. @RequestParam

    • 用于绑定请求参数到方法参数。
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
        
        @GetMapping("/greet")
        public String greet(@RequestParam(name = "name", required = false, defaultValue = "World") String name) {
            return "Hello, " + name + "!";
        }
    }
    
  2. @PathVariable

    • 用于将URL中的路径变量绑定到方法参数。
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
        
        @GetMapping("/user/{id}")
        public String getUserById(@PathVariable("id") String id) {
            return "User ID: " + id;
        }
    }
    
  3. @RequestBody

    • 用于将请求体绑定到方法参数,通常用于处理JSON或XML请求体。
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
        
        @PostMapping("/json")
        public String handleJson(@RequestBody String json) {
            return "Received JSON: " + json;
        }
    }
    
  4. @ResponseBody

    • 用于将方法返回值直接作为HTTP响应体返回,通常用于返回JSON或XML数据。
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
        
        @GetMapping("/message")
        @ResponseBody
        public String getMessage() {
            return "Hello, World!";
        }
    }
    

其他常用注解

  1. @ModelAttribute

    • 用于绑定请求参数到模型对象,并在视图中使用该对象。
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class MyController {
        
        @RequestMapping("/form")
        public String showForm(@ModelAttribute("user") User user) {
            return "userForm";
        }
    }
    
  2. @SessionAttributes

    • 用于将模型中的属性暂存到HTTP会话中。
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.SessionAttributes;
    
    @Controller
    @SessionAttributes("user")
    public class MyController {
        
        @RequestMapping("/login")
        public String login(@ModelAttribute("user") User user) {
            return "loginSuccess";
        }
    }
    

这些注解极大地简化了Spring MVC应用程序的开发,使代码更简洁、易读,并提高了开发效率。

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟主教

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值