@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")