【JavaEE进阶】SpringMVC中的常用注解和用法
    		       		warning:
    		            这篇文章距离上次修改已过433天,其中的内容可能已经有所变动。
    		        
        		                
                
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
@Controller
@RequestMapping("/example")
public class ExampleController {
 
    // 映射HTTP GET请求到指定的处理方法
    @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    public @ResponseBody String greeting() {
        return "Hello, World!";
    }
 
    // 映射HTTP POST请求到指定的处理方法
    @RequestMapping(value = "/farewell", method = RequestMethod.POST)
    public @ResponseBody String farewell() {
        return "Goodbye, World!";
    }
 
    // 映射HTTP PUT请求到指定的处理方法
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public @ResponseBody String update() {
        return "Record updated!";
    }
 
    // 映射HTTP DELETE请求到指定的处理方法
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public @ResponseBody String delete() {
        return "Record deleted!";
    }
}这个简单的例子展示了如何在Spring MVC中使用@Controller创建一个控制器类,并使用@RequestMapping注解来映射不同的HTTP请求到相应的处理方法上。同时,@ResponseBody注解被用于将返回值直接作为HTTP响应正文返回,通常用于返回JSON或XML格式的数据。
评论已关闭