注解之 @RestController 和 @RequestMapping
Controller 是 Spring 中最基本的组件,主要处理用户交互,一般每个业务逻辑都会有一个 Controller,供用户请求接口进行数据访问;@RequestMapping 注解用于绑定URI到具体处理器。二者相辅相成,共同完成前后端数据交互。
一、简介
IntelliJ IDEA version:2018.3
Spring Boot version: 2.1.4.RELEASE;
Java version:1.8。
Controller 是 Spring 中最基本的组件,主要处理用户交互,一般每个业务逻辑都会有一个 Controller,供用户请求接口进行数据访问。
@Controller
@RequestMapping("/user")
public class UserController {
private static Logger logger = LoggerFactory.getLogger(UserController.class);
/**
* 示例地址 http://localhost:8087/user/viewUser?ownerId=100
*
* @author Wiener
* @date 2019/5/8 11:27
*/
@RequestMapping("/viewUser")
@ResponseBody
public User viewUser(Long ownerId) {
logger.info("请求参数 ownerId = " + ownerId);
User user = new User();
user.setId(ownerId);
user.setName(" --> Lucy");
return user;
}
}
// 定义User Bean
public class User {
private Long id;
private String name; // omit getter、setter and toString
}
在上述示例中,@Controller 是标记在类UserController上面的,所以此类就是一个控制器类对象了。使用@RequestMapping("/user")标记控制器,用@RequestMapping(“/viewUser”) 标记方法,表示当请求“/user/viewUser”的时候访问的是UserController的viewUser方法,它返回了一个User 对象。这些在下文将会详细介绍。
二、@RestController介绍
三、@RequestMapping 配置URI映射
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
String[] value() default {};
String[] path() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
String[] consumes() default {};
String[] produces() default {};
}
注解@Target有两个属性,分别为 ElementType.METHOD 和 ElementType.TYPE,这表明@RequestMapping注解可以添加在类级别和方法级别上。
四、@RequestMapping属性介绍
- value属性
@RestController@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = {
"",
"/page",
"page*",
"view/*,**/msg"
})
String indexMultipleMapping() {
return "Hello from index multiple mapping.";
}
}
前面这段代码中,如下的这些 URI 都会由 indexMultipleMapping() 来处理:
localhost:8080/home
localhost:8080/home/page
localhost:8080/home/pageabc
localhost:8080/home/view/
localhost:8080/home/view/view
localhost:8080/home/view2/msg
这个示例同时说明了可以将多个请求映射到一个方法上去,只需要添加一个带有请求路径值列表的 @RequestMapping 注解就行了。
- params属性
@RequestMapping (value= "/testParams" , params={ "param1=value1" , "param2" , "!param3" })
public String testParams() {
System. out .println( "test Params..........." );
return "testParams" ;
}
在上面的代码中,我们用@RequestMapping 的params 属性指定了三个参数,这些参数都是针对请求参数而言的,它们分别表示参数param1 的值必须等于value1,param2 必须存在,值无所谓,param3 必须不存在,只有当请求“/testParams”并且满足指定的三个参数条件的时候才能访问到该方法。所以当请求/testParams?param1=value1¶m2=value2 的时候testParams函数能够正确响应;当请求/testParams?param1=value1¶m2=value2¶m3=value3 的时候就无响应,因为在@RequestMapping 的params 参数里面指定了参数param3 是不能存在的。
- method属性
@RequestMapping (value= "testMethod" , method={RequestMethod. GET , RequestMethod. DELETE })
public String testMethod() {
return "method" ;
}
在上面的代码中就使用method 参数限制请求方式,以GET 或DELETE 方法请求/testMethod 的时候才能访问testMethod 方法。
- headers属性
@RequestMapping (value= "testHeaders" , headers={ "host=localhost" , "Accept" })
public String testHeaders() {
return "headers" ;
}
在上面的代码中当请求/testHeaders 的时候只有当请求头包含Accept 信息,且请求的host 为localhost 的时候才能正确的访问到testHeaders 方法。
- produces和consumes属性
@PostMapping(value = "/cons", consumes = {
"application/JSON",
"application/XML"
})
public User getConsumes(Long ownerId) {
User user = new User();
user.setId(ownerId);
user.setName("Consumes attribute --> Lucy");
return user;
}
Reference
注解之 @RestController 和 @RequestMapping的更多相关文章
- SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别
@Controller 处理http请求 @Controller //@ResponseBody public class HelloController { @RequestMapping(valu ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别
SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...
- springmvc常用注解之@Controller和@RequestMapping
对于各种注解而言,排第一的当然是“@Controller”,表明某类是一个controller. “@RequestMapping”请求路径映射,如果标注在某个controller的类级别上,则表明访 ...
- 【#】Spring3 MVC 注解(二)---@RequestMapping
博客分类: spring MVC 1 问题:有多个 @RequestMapping @controller @RequestMapping("/aaa") ...
- Spring Boot常用注解总结
Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...
- SSM框架主要几个注解的位置
@Controller @Service @Repository @Component Controller (控制层) Service (业务层) daoImpl (实现层) 模糊注解 @Autow ...
- 前端-关于CORS跨域的解决方案,面向服务端
最近自己在写后台管理系统的时候,并没有采用jsp.freemaker.叶子等模板技术,而是由后端提供数据api,前端通过AJAX和JQuery来动态操作页面上的一些div.table元素,从而实现报表 ...
- 超详细 Spring @RequestMapping 注解使用技巧
@RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 在这篇文章中,你将会看到 @R ...
随机推荐
- (五)Activiti之获取流程定义图片和流程定义删除
一.获取流程定义图片 /** * 通过流程部署ID获取流程图图片 */ @Test public void getImageById()throws Exception{ InputStream in ...
- go语言interface学习
Go 中的 interface 所具有的最基本的功能:作为一种 abstract type,实现各种 concrete type 的行为统一. interface是一种类型.只有是实例化后才能调用in ...
- 华擎 J3455 主板装 Linux 系统
入手华擎J3455 ITX 主板,装备安装一个 redhat 来学习linux,及做一个家庭 web 服务器.但安装过程一波三折. 问题1.使用U盘引导不了,首先华擎这块板是 UEFI 板,用之前的老 ...
- Linux命令——diff、patch
简介 diff以行为单位比较不同ASCII文件差异,可以输出一组指令,用于指导如何更改一个文件使其与第二个文件相同.diff在软件开发时多用于比较新旧版本代码,和patch连用可以将文件间区别做成补丁 ...
- Windows 10版本区别
在msdn资源站上看到两个下载不同的区别,二者都内置专业版,不同之处在于: consumer_editions 版本包含:(个人使用 零售版) Home(家庭版); Education(教育版) ; ...
- synchronized 和 ReentrantLock 区别是什么?(未完成)
synchronized 和 ReentrantLock 区别是什么?(未完成)
- LeetCode刷题--有效的括号(简单)
题目描述 给定一个只包括 ' ( ' , ' ) ', ' { ' , ' } ' , ' [ ' , ' ] ' 的字符串,判断字符串是否有效.有效字符串需满足: 左括号必须用相同类型的右括号闭 ...
- 甘特图控件如何自定义绘图?DevExpress Winforms帮你忙
DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpr ...
- 详解Python 切片语法
Python的切片是特别常用的功能,主要用于对列表的元素取值.这篇文章主要介绍了详解Python 切片语法,需要的朋友可以参考下 Python的切片是特别常用的功能,主要用于对列表的元素取值.使用切片 ...
- file控件选择同一文件不触发change事件和img控件不改变src的情况下图片不刷新问题解决
最近跑来前端掺和了.. file控件的问题用 inputFile.value = ''; img控件的问题,在图片后面添加一串无意义的参数即可,例如: img.src = 'file:///' + 本 ...