SpringBoot 中常用注解及各种注解作用

本篇文章将介绍几种SpringBoot 中常用注解

其中,各注解的作用为:

@PathVaribale 获取url中的数据

@RequestParam 获取请求参数的值

@GetMapping 组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写

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

@PathVaribale 获取url中的数据

看一个例子,如果我们需要获取Url=localhost:8080/hello/id中的id值,实现代码如下:

@RestController
public class HelloController {

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

@RequestParam 获取请求参数的值

直接看一个例子,如下

@RestController
public class HelloController {

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

在浏览器中输入地址:localhost:8080/hello?id=1000,可以看到如下的结果:

当我们在浏览器中输入地址:localhost:8080/hello?id ,即不输入id的具体值,此时返回的结果为null。具体测试结果如下:

@GetMapping 组合注解

@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。该注解将HTTP Get 映射到 特定的处理方法上。

即可以使用@GetMapping(value = “/hello”)来代替@RequestMapping(value=”/hello”,method= RequestMethod.GET)。即可以让我们精简代码。

例子

@RestController
public class HelloController {
  //@RequestMapping(value="/hello",method= RequestMethod.GET)
  @GetMapping(value = "/hello")
  //required=false 表示url中可以不穿入id参数,此时就使用默认参数
  public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){
    return "id:"+id;
  }
}

@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";
  }
}

注解@RequestParam 和 @PathVarible的区别

@RequestParam是请求中的参数。如get?id=1

@PathVarible是请求路径中的变量如 get/id=1

总结

以上所述是小编给大家介绍的SpringBoot 中常用注解及各种注解作用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

您可能感兴趣的文章:

  • 详解spring boot mybatis全注解化
  • SpringBoot 注解事务声明式事务的方式
  • Spring Boot 基于注解的 Redis 缓存使用详解
  • 详解SpringBoot AOP 拦截器(Aspect注解方式)
  • 详解Spring Boot集成MyBatis(注解方式)
  • spring boot 的常用注解使用小结
(0)

相关推荐

  • spring boot 的常用注解使用小结

    @RestController和@RequestMapping注解 4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解.4.0之前的版本,Spring MVC的组件都使用@Controller来标识当前类是一个控制器servlet.使用这个特性,我们可以开发REST服务的时候不需要使用@Controller而专门的@RestController. 当你实现一个RESTful web services的时候,response将一直通过response

  • 详解SpringBoot AOP 拦截器(Aspect注解方式)

    常用用于实现拦截的有:Filter.HandlerInterceptor.MethodInterceptor 第一种Filter属于Servlet提供的,后两者是spring提供的,HandlerInterceptor属于Spring MVC项目提供的,用来拦截请求,在MethodInterceptor之前执行. 实现一个HandlerInterceptor可以实现接口HandlerInterceptor,也可以继承HandlerInterceptorAdapter类,两种方法一样.这个不在本文

  • 详解spring boot mybatis全注解化

    本文重点给大家介绍spring boot mybatis 注解化的实例代码,具体内容大家参考下本文: pom.xml <!-- 引入mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version

  • 详解Spring Boot集成MyBatis(注解方式)

    MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集.spring Boot是能支持快速创建Spring应用的Java框架.本文通过一个例子来学习Spring Boot如何集成MyBatis,而且过程中不需要XML配置. 创建数据库 本文的例子使用MySQL数据库,首先创建一个用户表,执行sql语句如下: CREATE TABLE IF NOT EXISTS user ( `id` INT(10) NOT NULL A

  • Spring Boot 基于注解的 Redis 缓存使用详解

    看文本之前,请先确定你看过上一篇文章<Spring Boot Redis 集成配置>并保证 Redis 集成后正常可用,因为本文是基于上文继续增加的代码. 一.创建 Caching 配置类 RedisKeys.Java package com.shanhy.example.redis; import java.util.HashMap; import java.util.Map; import javax.annotation.PostConstruct; import org.springf

  • SpringBoot 注解事务声明式事务的方式

    springboot 对新人来说可能上手比springmvc要快,但是对于各位从springmvc转战到springboot的话,有些地方还需要适应下,尤其是xml配置.我个人是比较喜欢注解➕xml是因为看着方便,查找方便,清晰明了.但是xml完全可以使用注解代替,今天就扒一扒springboot中事务使用注解的玩法. springboot的事务也主要分为两大类,一是xml声明式事务,二是注解事务,注解事务也可以实现类似声明式事务的方法,关于注解声明式事务,目前网上搜索不到合适的资料,所以在这里

  • SpringBoot 中常用注解及各种注解作用

    本篇文章将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写 @RestController是@ResponseBody和@Controller的组合注解. @PathVaribale 获取url中的数据 看一个例子,如果我们需要获取Url=localhost:

  • SpringMVC中常用参数校验类注解使用示例教程

    目录 一.环境准备 二.常用的校验注解及示例 三.校验类方法中的普通参数 四.校验类方法中的自定义对象 五.关于@Valid和@Validated的区别联系 六.分组校验 七.自定义校验注解 一.环境准备 在项目中添加以下依赖 gradle org.hibernate:hibernate-validator:5.3.5.Final maven <dependency> <groupId>org.hibernate</groupId> <artifactId>

  • java开发MyBatis中常用plus实体类注解符详解

    目录 mybatis-plus常用注解符 1. 表名注解(@TableName) 2. 主键注解(@TableId) 3. 属性注解(@TableField) mybatis-plus常用注解符 1. 表名注解(@TableName) 作用:实体类和数据库中表建立对应关系:如 @TableName("thotset") public class HotsetEntity implements Serializable { private static final long serial

  • Springboot中使用lombok的@Data注解方式

    目录 Springboot 使用lombok的@Data注解 idea安装lombok插件 创建项目,编写实体类 编写测试类 测试结果 springBoot 注解@Data注入失败 一.Files--Seetings--Plugins 二.如果重启后仍注入失败 Springboot 使用lombok的@Data注解 idea安装lombok插件 点击setting,选择plugins,搜索lombok安装即可. 创建项目,编写实体类 安装好插件后需要重启idea,然后创建一个springboot

  • SpringBoot中@Pattern注解对时间格式校验方式

    目录 SpringBoot @Pattern注解对时间格式校验 1.需求背景 2.实现案例 @Pattern的用法 下面是常用的正则表达式 SpringBoot @Pattern注解对时间格式校验 1.需求背景 有一个需求,在前端传过来的时间格式的字符串进行校验,是否符合"yyyy-MM-dd HH:mm:ss",在SpringBoot中当然可以用@Datetimeformat注解来进行验证,但字段的属性得用Date类型,由于我的项目中该字段用了String类型,需要对类型进行转换不太

  • 详解在springboot中使用Mybatis Generator的两种方式

    介绍 Mybatis Generator(MBG)是Mybatis的一个代码生成工具.MBG解决了对数据库操作有最大影响的一些CRUD操作,很大程度上提升开发效率.如果需要联合查询仍然需要手写sql.相信很多人都听说过微服务,各个微服务之间是松耦合的.每个微服务仅关注于完成一件任务并很好地完成该任务.在一个微服务的开发过程中很可能只关注对单表的操作.所以MBG在开发过程中可以快速的生成代码提升开发效率. 本文将说到在springboot的项目中如何去配置(XML形式和Java配置类形式)和使用M

  • 详谈PHP面向对象中常用的关键字和魔术方法

    PHP面向对象中常用的关键字   final 1.final不能修饰成员属性(类中常量不是用这个关键字) 2.final只能修饰类和方法 作用: 使用final修饰的类不能被子类继承 使用final修饰的方法不能被子类覆盖 用来限制类不被继承,方法不被覆盖就使用final <?php //final修饰的类不能被继承 final class Person{ var $name; var $age; var $sex; function __construct($name,$age,$sex){

  • springboot中一些比较常用的注解总结

    springboot常用注解 1.@SpringBootApplication 这个注解是Spring Boot最核心的注解,用在 Spring Boot的主类上,标识这是一个 Spring Boot 应用,用来开启 Spring Boot 的各项能力.实际上这个注解是@Configuration,@EnableAutoConfiguration,@ComponentScan三个注解的组合.由于这些注解一般都是一起使用,所以Spring Boot提供了一个统一的注解@SpringBootAppl

  • SpringBoot中的Condition包下常用条件依赖注解案例介绍

    目录 一.@ConditionalOnClass() Spring中存在指定class对象时,注入指定配置 1.首先引入pom依赖 2.实体类测试对象 3.定义@ConditionalOnClass()配置类 4.启动类测试 二.注入指定配置 1.首先引入pom依赖 2.实体类测试对象 3.定义@ConditionalOnMissingClass()配置类 4.启动类测试 三.加载指定配置 1.首先引入pom依赖 2.实体类测试对象 2.1 引入条件判断实体类 3.定义@ConditionalO

  • springBoot系列常用注解(小结)

    @PropertySource 作用是:对自定义的properties文件加载 使用:@PropertySource(value={"classpath:people.properties"})或者@PropertySource(value="classpath:people.properties") properties文件,获取到值乱码问题 乱码解决: file ->settings -->file encoding--> 勾选Transpar

随机推荐