当前位置:   article > 正文

SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍_springboot @requestmapping忽略后缀

springboot @requestmapping忽略后缀

1.Controller 一般要和http请求配合使用 。

  1. @Controller 处理http请求
  2. @Controller
  3. //@ResponseBody
  4. public class HelloController {
  5.     @RequestMapping(value="/hello",method= RequestMethod.GET)
  6.     public String sayHello(){
  7.         return "hello";
  8.     }
  9. }

2.若直接使用@Controller启动springboot项目,输入地址时会报Whitelabel Error Page错误。

出现这种情况的原因在于:没有使用模版。即@Controller 用来响应页面,@Controller必须配合模版来使用。spring-boot 支持多种模版引擎包括: 
1,FreeMarker 
2,Groovy 
3,Thymeleaf (Spring 官网使用这个) 
4,Velocity 
5,JSP (貌似Spring Boot官方不推荐,STS创建的项目会在src/main/resources 下有个templates 目录,这里就是让我们放模版文件的,然后并没有生成诸如 SpringMVC 中的webapp目录)

本文以Thymeleaf为例介绍使用模版,具体步骤如下:

第一步:在pom.xml文件中添加如下模块依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

第二步:修改控制器代码,具体为:

  1. /**
  2.  * Created by wuranghao on 2017/4/7.
  3.  */
  4. @Controller
  5. public class HelloController {
  6.     @RequestMapping(value="/hello",method= RequestMethod.GET)
  7.     public String sayHello(){
  8.         return "hello";
  9.     }
  10. }



第三步:在resources目录的templates目录下添加一个hello.html文件,具体工程目录结构如下:

其中,hello.html文件中的内容为:

 <h1>wojiushimogui</h1>

这样,再次运行此项目之后,在浏览器中输入:localhost:8080/hello

就可以看到hello.html中所呈现的内容了。

因此,我们就直接使用@RestController注解来处理http请求来,这样简单的多。
 

@RestController
Spring4之后新加入的注解,原来返回json需要@ResponseBody和@Controller配合。

即@RestController是@ResponseBody和@Controller的组合注解。

  1. @RestController
  2. public class HelloController {
  3.     @RequestMapping(value="/hello",method= RequestMethod.GET)
  4.     public String sayHello(){
  5.         return "hello";
  6.     }
  7. }
  8. 与下面的代码作用一样
  9. @Controller
  10. @ResponseBody
  11. public class HelloController {
  12.     @RequestMapping(value="/hello",method= RequestMethod.GET)
  13.     public String sayHello(){
  14.         return "hello";
  15.     }
  16. }


@RequestMapping 配置url映射
@RequestMapping此注解即可以作用在控制器的某个方法上,也可以作用在此控制器类上。

当控制器在类级别上添加@RequestMapping注解时,这个注解会应用到控制器的所有处理器方法上。处理器方法上的@RequestMapping注解会对类级别上的@RequestMapping的声明进行补充。

看两个例子

例子一:@RequestMapping仅作用在处理器方法上

  1. @RestController
  2. public class HelloController {
  3.     @RequestMapping(value="/hello",method= RequestMethod.GET)
  4.     public String sayHello(){
  5.         return "hello";
  6.     }
  7. }


以上代码sayHello所响应的url=localhost:8080/hello。

例子二:@RequestMapping仅作用在类级别上

  1. /**
  2.  * Created by wuranghao on 2017/4/7.
  3.  */
  4. @Controller
  5. @RequestMapping("/hello")
  6. public class HelloController {
  7.     @RequestMapping(method= RequestMethod.GET)
  8.     public String sayHello(){
  9.         return "hello";
  10.     }
  11. }


以上代码sayHello所响应的url=localhost:8080/hello,效果与例子一一样,没有改变任何功能。

例子三:@RequestMapping作用在类级别和处理器方法上

  1. /**
  2.  * Created by wuranghao on 2017/4/7.
  3.  */
  4. @RestController
  5. @RequestMapping("/hello")
  6. public class HelloController {
  7.     @RequestMapping(value="/sayHello",method= RequestMethod.GET)
  8.     public String sayHello(){
  9.         return "hello";
  10.     }
  11.     @RequestMapping(value="/sayHi",method= RequestMethod.GET)
  12.     public String sayHi(){
  13.         return "hi";
  14. }
  15. }



这样,以上代码中的sayHello所响应的url=localhost:8080/hello/sayHello。

sayHi所响应的url=localhost:8080/hello/sayHi。

从这两个方法所响应的url可以回过头来看这两句话:当控制器在类级别上添加@RequestMapping注解时,这个注解会应用到控制器的所有处理器方法上。处理器方法上的@RequestMapping注解会对类级别上的@RequestMapping的声明进行补充。

最后说一点的是@RequestMapping中的method参数有很多中选择,一般使用get/post.

小结
本篇博文就介绍了下@Controller/@RestController/@RequestMappping几种常用注解,下篇博文将介绍几种如何处理url中的参数的注解@PathVaribale/@RequestParam/@GetMapping。

其中,各注解的作用为:

@PathVaribale 获取url中的数据

@RequestParam 获取请求参数的值

@GetMapping 组合注解
--------------------- 
作者:HelloWorld_EE 
来源:CSDN 
原文:https://blog.csdn.net/u010412719/article/details/69710480 
版权声明:本文为博主原创文章,转载请附上博文链接!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/194529
推荐阅读
相关标签
  

闽ICP备14008679号