SpringBoot——Controller层常用注解作用
在SpringMVC当中,提供了一个注解@Controller,以此标记一个类是Controller,然后结合@RequestMapping和@RequestParam等注解进行URL和Contronller方法之间的映射,这样可以使Controller被外界访问到
如下图程序,一个Controller类的结构如下,使用@Controller进行标记这是一个Controller
[code]@Controller public class HomeController { @Autowired NewsService newsService; @Autowired UserService userService; @Autowired HostHolder hostHolder; private List<ViewObject> getNews(int userId, int offset, int limit) { List<News> newsList = newsService.getLatestNews(userId, offset, limit); List<ViewObject> vos = new ArrayList<>(); for (News news : newsList) { ViewObject vo = new ViewObject(); vo.set("news", news); vo.set("user", userService.getUser(news.getUserId())); vos.add(vo); } return vos; } @RequestMapping(path = {"/", "/index"}, method = {RequestMethod.GET, RequestMethod.POST}) public String index(Model model, @RequestParam(value = "pop", defaultValue = "0") int pop) { model.addAttribute("vos", getNews(0, 0, 10)); model.addAttribute("pop", pop); return "home"; } @RequestMapping(path = {"/user/{userId}"}, method = {RequestMethod.GET, RequestMethod.POST}) public String userIndex(Model model, @PathVariable("userId") int userId) { model.addAttribute("vos", getNews(userId, 0, 10)); return "home"; } }
@RequestMapping:
@RequestMapping(“/path”)表示该控制器处理所有“/path”的URL请求。RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。
用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。该注解有六个属性:
params:指定request中必须包含某些参数值是,才让该方法处理。
headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。
value:指定请求的实际地址,指定的地址可以是URI Template 模式
method:指定请求的method类型, GET、POST、PUT、DELETE等
consumes:指定处理请求的提交内容类型(Content-Type),如application/json,text/html;
produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回
@PathVariable:
若在path当中使用{XXX}进行传递参数,则要使用@pathVariable("XXX")进行获取
[code] @RequestMapping(path = {"/user/{userId}"}, method = {RequestMethod.GET, RequestMethod.POST}) public String userIndex(Model model, @PathVariable("userId") int userId) { model.addAttribute("vos", getNews(userId, 0, 10)); return "home"; }
@RequestParam:
[code] @RequestMapping(path = {"/", "/index"}, method = {RequestMethod.GET, RequestMethod.POST}) public String index(Model model, @RequestParam(value = "pop", defaultValue = "0") int pop) { model.addAttribute("vos", getNews(0, 0, 10)); model.addAttribute("pop", pop); return "home"; }
使用@RequestParam进行request内容的获取,通过指定键值value = "XXX", 获取键值为“XXX”的值
@CookieValue:
以下为获取名称为"nowcoderid"的cookie的值
[code]@RequestMapping(value = {"/response"}) @ResponseBody public String response(@CookieValue(value = "nowcoderid", defaultValue = "a") String nowcoderId, @RequestParam(value = "key", defaultValue = "key") String key, @RequestParam(value = "value", defaultValue = "value") String value, HttpServletResponse response) { response.addCookie(new Cookie(key, value)); response.addHeader(key, value); return "NowCoderId From Cookie:" + nowcoderId; }
@ReponseBody表示返回是一个Body,不调用templates中模板进行渲染
@ExceptionHandler(Exception.class):用在方法上面表示遇到这个异常就执行以下方法。
- SpringBoot 中常用注解及各种注解作用
- Spring boot常用注解解释
- spring各jar包作用于spring常用注解(转)
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
- spring、springboot不常用但很有效的注解(一直要完善)
- Springboot 常用注解
- springBoot学习之常用注解 --1
- Springboot常用注解小结
- springboot常用注解的使用
- springboot常用注解集锦
- springboot常用注解
- Spring Boot常用注解
- Springboot 常用注解
- springboot(二)一springboot常用注解
- SpringBoot中@EnableAutoConfiguration注解的作用
- Springboot 常用注解
- springboot 一些常用的注解介绍
- Spring Boot常用注解小结
- SpringBoot 常用注解的通俗解释
- spring各jar包作用于spring常用注解