spring-boot 注解解析
package com.hllq.quan.controller; import com.hllq.quan.mapper.WeiboUserMapper;
import com.hllq.quan.model.WeiboUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import com.hllq.quan.mapper.WeiboUserMapper; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; @RestController
public class WeiboUserController { @Autowired
private WeiboUserMapper weiboUserMapper; @RequestMapping("/selectall")
public List<WeiboUser> selectAll(){
return weiboUserMapper.selectAll();
} // @RequestMapping("/selectbyid/{id}")
// public WeiboUser selectById(@PathVariable("id") int id){
// return weiboUserMapper.selectById(id);
// } @InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
} // 获取url参数值,默认方式,需要方法参数名称和url参数保持一致http://localhost:8080/selectbyid?id=2
@RequestMapping(value = "/selectbyid",method = RequestMethod.GET)
public WeiboUser selectById(@RequestParam Integer id){
return weiboUserMapper.selectById(id);
} @RequestMapping(value = "/insert", method = RequestMethod.GET)
public int insertWeiboUser( WeiboUser weiboUser){
weiboUserMapper.insertWeiboUser(weiboUser);
return 1;
} @RequestMapping(value = "/updatenamebyid" ,method = RequestMethod.GET)
public void updateWeiboUserName(WeiboUser weiboUser){
weiboUserMapper.updateWeiboUserName(weiboUser);
} @RequestMapping(value = "/deletebyid",method = RequestMethod.GET)
public void deleteById(@RequestParam Integer id){
weiboUserMapper.deleteById(id);
}
}
@RestController:用于标注控制层组件(如struts中的action),@ResponseBody和@Controller的合集
@RequestMapping:提供路由信息,负责URL到Controller中的具体函数的映射。
value: 指定请求的实际地址
method: 指定请求的method类型, GET、POST、PUT、DELETE等
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回
params: 指定request中必须包含某些参数值是,才让该方法处理
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求
@Autowired:自动导入依赖的bean
@PathVariable指定URL变量名,前提是RequesrMapping需要用{}表面他的变量部分
@RequestMapping("/users/{username}")
{username}就是我们定义的变量规则,username是变量的名字
获取的时候: public String userProfile(@PathVariable String username)
或者: public String userProfile(@PathVariable("username") String username)
存在多个的时候:
@RequestMapping("/user/{username}/blog/{blogId}")
@ResponseBody
public String getUerBlog(@PathVariable String username , @PathVariable int blogId)
也可以使用正则表达式,匹配更加精确的变量,定义语法是{变量名:正则表达式}
@RequestMapping("/user/{username:[a-zA-Z0-9_]+}/blog/{blogId}")
@RequestParam:将请求参数绑定到你控制器的方法参数上
语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””) value:参数名 required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。 defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,
在需要日期转换的Controller中使用SpringMVC的注解@initbinder和Spring自带的WebDateBinder类来操作。
WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set这个值的时候,如果set的这个属性是个对象,
Spring就会去找到对应的editor进行转换,然后再SET进去。
spring-boot 注解解析的更多相关文章
- Spring boot注解(annotation)含义详解
Spring boot注解(annotation)含义详解 @Service用于标注业务层组件@Controller用于标注控制层组件(如struts中的action)@Repository用于标注数 ...
- Spring Boot 注解之ObjectProvider源码追踪
最近依旧在学习阅读Spring Boot的源代码,在此过程中涉及到很多在日常项目中比较少见的功能特性,对此深入研究一下,也挺有意思,这也是阅读源码的魅力之一.这里写成文章,分享给大家. 自动配置中的O ...
- Spring Boot注解大全,一键收藏了!
本文首发于微信公众号[猿灯塔],转载引用请说明出处 今天是猿灯塔“365天原创计划”第5天. 今天呢!灯塔君跟大家讲: Spring Boot注解大全 一.注解(annotations)列表 @Spr ...
- Spring boot 注解简单备忘
Spring boot 注解简单备忘 1.定义注解 package com.space.aspect.anno;import java.lang.annotation.*; /** * 定义系统日志注 ...
- 73. Spring Boot注解(annotation)列表【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 针对于Spring Boot提供的注解,如果没有好好研究一下的话,那么想应用自如Spring Boot的话,还是有点困难的,所以我们这小节,说说S ...
- Spring 缓存注解解析过程
Spring 缓存注解解析过程 通过 SpringCacheAnnotationParser 的 parseCacheAnnotations 方法解析指定方法或类上的缓存注解, @Cacheable ...
- Spring Boot 注解详解
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- Spring boot注解使用
1:@SpringBootApplication 注解 a:scanBasePackages 与scanBasePackageClasses配置Spring启动时扫描的包路径或者扫描的字节码文件 b: ...
- spring mvc注解和spring boot注解
1 spring mvc和spring boot之间的关系 spring boot包含spring mvc.所以,spring mvc的注解在spring boot总都是可以用的吗? spring b ...
- spring boot注解
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
随机推荐
- 华为模拟器在三层交换机上实现dhcp的配置
<Huawei>sysEnter system view, return user view with Ctrl+Z.[Huawei]sys sw1[sw1]dhcp enable Inf ...
- 小甲鱼 python——第一课作业!
0: python是脚本语言把?虽然不是很清楚什么是脚本语言就是了.复制一下: 脚本语言(英语:Scripting language)是为了缩短传统的"编写.编译.链接.运行"( ...
- 100G/40G/25G/10G网络测试解决方案
一.100G概述 随着CDN等视频直播业务和P2P业务的快速发展,带宽的要求越来越高.当前5G业务势头正盛,其基于400G的主干网络通信业务也在积极部署之中.但当前在很多的业务场景中,100G系统的部 ...
- .Net 新一代编译器 Roslyn 会带来怎样的影响?
.Net 新一代编译器 Roslyn 会带来怎样的影响? Roslyn是微软创建的一个.NET编译器平台,该项目于2014年4月3日开源. 最初 C# 语言的编译器是用 C++ 编写的,后来微软推出了 ...
- centos7 配置nginx
安装是需要的环境 一. gcc 安装安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装: yum install gcc-c++ 二. PCR ...
- [炼丹术]DeepLabv3+训练模型学习总结
DeepLabv3+训练模型学习总结 一.DeepLabs3+介绍 DeepLabv3是一种语义分割架构,它在DeepLabv2的基础上进行了一些修改.为了处理在多个尺度上分割对象的问题,设计了在级联 ...
- 面试中的MySQL主从复制|手撕MySQL|对线面试官
关注微信公众号[程序员白泽],进入白泽的知识分享星球 前言 作为<手撕MySQL>系列的第三篇文章,今天讲解使用bin log实现主从复制的功能.主从复制也是MySQL集群实现高可用.数据 ...
- JAVA_Scanner 键盘输入
键盘输入语句 介绍:在编程中,需要接收用户输入的数据,就可以使用键盘输入语句来获取.Input.java , 需要一个 扫描器(对象), 就是 Scanner 步骤: 导入该类的所在包, java.u ...
- JZ-038-二叉树的深度
二叉树的深度 题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 题目链接: 二叉树的深度 代码 /** * 标题:二叉 ...
- js识别手机型号做业务判断
navigator为Window对象的一个属性,指向了一个包含浏览器相关信息的对象. navigatot中包含了一些常用到的属性,如: navigator.appVersion 浏览器的版本号 nav ...