SpringBoot 注解 @Controller/@RestController/@RequestMapping

@Controller 处理http请求
@Controller
//@ResponseBody
public class HelloController {html

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

若是直接使用@Controller这个注解,当运行该SpringBoot项目后,在浏览器中输入:local:8080/hello,会获得以下错误提示: web


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

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

第一步:在pom.xml文件中添加以下模块依赖:浏览器

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

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


/**
 * Created by wuranghao on 2017/4/7.
 */
@Controller
public class HelloController {webapp

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

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

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

 <h1>wojiushimogui</h1>

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

就能够看到hello.html中所呈现的内容了。

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

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

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

@RestController
public class HelloController {

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

与下面的代码做用同样

@Controller
@ResponseBody
public class HelloController {

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

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

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

看两个例子

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

@RestController
public class HelloController {

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

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

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

/**
 * Created by wuranghao on 2017/4/7.
 */
@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

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

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

/**
 * Created by wuranghao on 2017/4/7.
 */
@RestController
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(value="/sayHello",method= RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
    @RequestMapping(value="/sayHi",method= RequestMethod.GET)
    public String sayHi(){
        return "hi";
    }
}

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

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

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

最后说一点的是@RequestMapping中的method参数有不少中选择,通常使用get/post.

 

属性参数示例

@RequestMapping(value = {"/modifyGet.do","/modifyGet1.do"}, method={RequestMethod.POST, RequestMethod.GET},

          consumes={"application/json"}, produces={"application/json"}, params={"name=mike","pwd=123456"},headers={"a=1"}) 

method 若是定义了某一种或多种请求方式,则只能已定义的方式进行请求,若没定义则各类方式都可请求。

 

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

其中,各注解的做用为:

@PathVaribale 获取url中的数据

@RequestParam 获取请求参数的值

@GetMapping 组合注解  

相关文章
相关标签/搜索