SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别

@Controller 处理http请求

@Controller
//@ResponseBody
public class HelloController {

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

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

出现这种情况的原因在于:没有使用模版。即@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>

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


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

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

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

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

 <h1>wojiushimogui</h1>

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

就可以看到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.

小结

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

其中,各注解的作用为:

@PathVaribale 获取url中的数据

@RequestParam 获取请求参数的值

@GetMapping 组合注解

原文地址:https://www.cnblogs.com/Jeremy2001/p/11108225.html

时间: 2024-08-30 03:06:55

SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别的相关文章

springboot中常用注解总结

1.@RestController(组合注解):标注在类上,等价于@Controller和@Responsebody @Controller:将该类标记为Controller层的类,并且注入到Spring容器中 @Responsebody:注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或是 XML数据. 2.@SpringBootApplication:该注解标注标注在某个类上,代表这个类是

Spring MVC 常用注解@Controller,@RequestMapping,Model和ModelAndView

[email protected] 用于指示Spring类的实例是一个控制器.Controller接口的实现类只能处理一个单一请求动作,而@Controller注解的控制器可以支持同时处理多个请求动作,更加灵活.Spring使用扫描机制查找应用程序中所有基于注解的控制器类.分发处理器会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping注解,而使用@RequestMapping注解的方法才是真正处理请求的处理器.为了保证能找到控制器,需要完成两件事情: 在Spring

spring注解开发中常用注解以及简单配置

一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向spring容器中注入bean对象, 然而,通过注解编程可以缩短我们开发的时间,简化程序员的代码编写. 2.如何开启注解开发:最常用的方法是使用<mvc:annotation-driven/>来开启注解编程(用一个标签配置了spring注解编程的映射器和适配器,同时配置了许多的参数) 3.如何将有注解的be

Spring中@Component注解,@Controller注解详解(网摘)

在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式, 只需要添加几行自动注入的的配置,便可以完成Service层,Controller层等等的注入配置. 使用过程中,在Service层中的实现类头上加@Compopnet注解,在Controller类头加@Controller注解,便完成了配置. 例如 在Controller中当我们调用某个Service时就不需要Set方法了,直接通过@Autowried 注解对Service

Spring中常用注解的介绍

spring中使用注解时配置文件的写法: <?xml version="1.0" encoding="UTF-8"?> <span style="font-size:18px;"><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-in

springMVC中的注解@RequestParam与@PathVariable的区别

@PathVariable绑定URI模板变量值 @PathVariable是用来获得请求url中的动态参数的 @PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上.//配置url和方法的一个关系@RequestMapping("item/{itemId}") /* @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,类似于struts的action请求* @responsebody表示该方法的返回结果直接写入HTTP

我爱Java系列之---【SpringBoot中常用的注解和两种注入方式】

@EnableConfigurationProperties(DataSourceProperties.class) 来声明要使用DataSourceProperties 这个类并初始化该类对象,该类不用放在IOC容器中,可以通过该注解直接使用. [email protected]:一般写在类上边,通过该注解将当前类初始化到Spring的IOC容器中,其他类若想调用,直接用@Autowired去容器中拿. [email protected]: 一般写在类上边,标明该类是一个配置类,被注解的类内部

Spring MVC中常用注解之@SessionAttributes @ModelAttribute详解

@RequestMapping(value = "/doupdate") public String doupdate(@RequestParam(value = "id") String id, Model model) { model.addAttribute("person", personService.getPersonById(id)); return "editpage"; } spring 2.0 定义了一个

ssm中常用注解

[email protected]:加在controller类上,作用是使所有的返回值自动转换为json对象,相当于在每个方法上加上@ResponseBody @RequestMapping("/goods") @RestController public class GoodsController [email protected]:加在变量上,作用是注入dubbo的分布式服务 @Reference private GoodsService goodsService; [email