@Controller://处理http请求
@RestController://返回json
@RequestMapping://配置url映射
@RestController://返回json
@RequestMapping://配置url映射
命令是要配合模板使用的,在pom中引入官方thymeleaf(用来开发html)
在resources/templates中新建index.html,注意:在类前加入@Controller同时在方法返回return "index";
在resources/templates中新建index.html,注意:在类前加入@Controller同时在方法返回return "index";
-当需要访问localhost:8080/hello和localhost:8080/hi的时候需要都可以访问say()方法怎么办呢?
@RequestMapping(Value = {"/hello","/hi"},method = RequestMethod.GET)如果我们把类注释为@RequestMapping("/hello"),把方法注释为
@RequestMapping(value = "/say", method = RequestMethod.GET)这样url可以localhost://8080/hello是访问不到方法的,必须localhost://8080/hello/say
@RequestMapping(value = "/{id}/say", method = RequestMethod.GET)
public String say(@PathVariable("id") Integer myid){
return "id:" + myid;
}这样路径中的{id}可以被提取出来-如果路径是这样的:localhost:8080/say?id=100,怎样提取出id呢?
@RequestMapping(value = "/say", method = RequestMethod.GET)
public String say(@RequestParam("id") Integer myid){
return "id:" + myid;
}-那我们要id可以被预先设定呢?public String say(@RequestParam(value = "id",required = false, defaultValue = "0") Integer myid)-@RequestMapping的简化
@RequestMapping(value = "/say", method = RequestMethod.GET)=@GetMapping(value = "/say")
本文详细介绍了 Spring MVC 中的路径映射方法,包括如何使用 @RequestMapping 和其衍生注解来处理 HTTP 请求。文章还展示了如何通过 @PathVariable 和 @RequestParam 提取 URL 参数,并提供了默认参数设置的方法。
1447

被折叠的 条评论
为什么被折叠?



