@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam

原文链接:http://blog.sina.com.cn/s/blog_6d3c1ec601017q4l.html


下列参数一般都和@RequestMapping配合使用。

 

A@CookieValue

org.springframework.web.bind.annotation.CookieValue

public @interface CookieValue

Annotation which indicates that a method parameter should be bound to an HTTP cookie. Supported for annotated handler methods in Servlet and Portlet environments.

这个注释表示一个方法参数绑定到一个HTTP cookie。支持ServletPortlet环境。

The method parameter may be declared as type Cookie or as cookie value type (String,int,etc).

这个方法的参数可声明为Cookie类型或String,int等。

A.1@CookieValue的属性

String value

The name of the cookie to bind to.

绑定的cookie名称。

boolean required

Whether the header is required.

Default is true,leading to an exception being thrown in case the header is missing in the request. Switch this to false if you prefer a null in case of the missing header.

Head是否需要。默认是true,请求中头丢失将抛出一个异常。False,请求中头丢失将返回null

Alternatively,provide a defaultValue,which implicitly sets this flag to false.

因此,提供一个defaultValue

String defaultValue

The default value to use as a fallback. Supplying a default value implicitly sets required() to false.

requiredfalse,请求中头丢失将返回这个值。

B@PathVariable

Annotation which indicates that a method parameter should be bound to a URI template variable. Supported for RequestMapping annotated handler methods in Servlet environments.

    这个参数指出方法的一个参数绑定到一个URI template变量。在Servlet环境中的被@RequestMapping注释的处理器方法。

B.1@PathVariable的属性

value

The URI template variable to bind to.

绑定URI template变量。

举例说明

@Controller

public class HelloWorldController {    @RequestMapping("/helloWorld/{userId}")

public String helloWorld(ModelMap model,@PathVariable("userId") String userId) {

       model.addAttribute("attributeName",userId);

       return "helloWorld";

    }

}

URI template变量和方法的参数名称一样时,可以省略value的定义,@PathVariable达到同样的效果。

 

C@RequestBody

Annotation which indicates that a method parameter should be bound to the web request body. Supported for annotated handler methods in Servlet environments.

这个注释它指示一个方法的参数绑定到一个web请求的body。它支持Servlet环境中的注释处理器方法。

class HelloWorldController {

    "/hello.do")   

    public String helloWorld(Model model,100);font-size:10pt;">@RequestBody String reqBody) {

"message",reqBody);

    }

}

这时这个参数reqBody的值是请求页面的form表单的所有值。

 

D@ RequestHeader

Annotation which indicates that a method parameter should be bound to a web request header. Supported for annotated handler methods in Servlet and Portlet environments.

这个注释它指示一个方法的参数绑定到一个web请求的头信息。它支持ServletPortlet环境中的注释处理器方法。

D.1@ RequestHeader的属性

String defaultValue

The default value to use as a fallback.

默认返回值。

Boolean required

Whether the header is required.

是否需要header

String value

The name of the request header to bind to.

绑定的请求头名称。

@RequestHeader("Accept") String info) {

info);

这时这个参数info将获得请求的Accept头信息。

E@RequestParam

org.springframework.web.bind.annotation.RequestParam

Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in Servlet and Portlet environments.

这个参数指出一个方法的参数应绑定到一个web请求的参数。支持ServletPortlet环境下注释处理器的方法。

E.1@RequestParam的属性

E.1.1value

The name of the request parameter to bind to.

绑定的请求参数的名称。

@RequestParam(value="abc")等同于@RequestParam("abc")

E.1.2required

Whether the parameter is required.

是否需要参数。

Default is true,leading to an exception thrown in case of the parameter missing in the request. Switch this to false if you prefer a null in case of the parameter missing.

默认为true,若请求中没有参数会导致抛出一个异常。若设置为false,若请求中没有参数就会返回null

Alternatively,which implicitly sets this flag to false.

required=false时,最好设置一个defaultValue默认值。

@RequestParam(value = "abc",required=false)

E.1.3defaultValue

The default value to use as a fallback. Supplying a default value implicitly sets required() to false.

required=false时,设定默认值。

"/a")

"/b")

@RequestParam("a") String abc) {

 

F@ResponseBody

Annotation which indicates that a method return value should be bound to the web response body. Supported for annotated handler methods in Servlet environments.

这个注释它指示一个方法的返回值应该绑定到一个web响应的body中。它支持Servlet环境中的注释处理器方法。

应用@ResponseBody将会跳过视图处理,而是调用合适HttpMessageConverter,将返回值写入输出流。

@ResponseBody

public String helloWorld() {

或者这样定义

"/a/b")

public @ResponseBody String helloWorld() {

这时访问/a/b时,不是返回一个view名为helloWorld的视图,而是作出一个响应,其内容为helloWorld


原文地址:https://blog.csdn.net/thlzjfefe" target="_blank" rel="noopener" title="thlzjfefe">thlzjfefe</a> <img class="article-time-img article-heard-img" src="https://csdnimg.cn/release/blo

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


今天小编给大家分享的是Springboot下使用Redis管道(pipeline)进行批量操作的介绍,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起...
本篇文章和大家了解一下springBoot项目常用目录有哪些。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。springBoot项目常用目录springBoot项...
本篇文章和大家了解一下Springboot自带线程池怎么实现。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。一: ThreadPoolTaskExecuto1 ThreadP...
这篇文章主要介绍了SpringBoot读取yml文件有哪几种方式,具有一定借鉴价值,需要的朋友可以参考下。下面就和我一起来看看吧。Spring Boot读取yml文件的主要方式...
今天小编给大家分享的是SpringBoot配置Controller实现Web请求处理的方法,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧...
本篇文章和大家了解一下SpringBoot实现PDF添加水印的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。简介PDF(Portable Document Form...
本篇文章和大家了解一下解决Springboot全局异常处理与AOP日志处理中@AfterThrowing失效问题的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有...
本篇文章和大家了解一下IDEA创建SpringBoot父子Module项目的实现方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。目录前言1. 软硬件环...
今天小编给大家分享的是springboot获取项目目录路径的方法,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收...
本篇内容主要讲解“SpringBoot+Spring Security无法实现跨域如何解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面...
这篇文章主要介绍“vue怎么发送请求到springboot程序”,在日常操作中,相信很多人在vue怎么发送请求到springboot程序问题上存在疑惑,小编查阅了各式资料,整理...
本篇内容主要讲解“Springboot内置的工具类CollectionUtils如何使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家...
本文小编为大家详细介绍“SpringBoot上传文件大小受限如何解决”,内容详细,步骤清晰,细节处理妥当,希望这篇“SpringBoot上传文件大小受限如何解决”文章能帮...
本文小编为大家详细介绍“springboot拦截器如何创建”,内容详细,步骤清晰,细节处理妥当,希望这篇“springboot拦截器如何创建”文章能帮助大家解决疑惑,下面...
本文小编为大家详细介绍“Hikari连接池使用SpringBoot配置JMX监控的方法是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“Hikari连接池使用SpringBoot配...
今天小编给大家分享一下SpringBoot如何使用Sa-Token实现权限认证的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大...
这篇文章主要介绍“SpringBoot如何集成SFTP客户端实现文件上传下载”,在日常操作中,相信很多人在SpringBoot如何集成SFTP客户端实现文件上传下...
本篇内容主要讲解“Springboot插件怎么开发”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Springboot插件怎
这篇文章主要介绍“Springboot怎么解决跨域请求问题”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇...
今天小编给大家分享一下如何在SpringBoot2中整合Filter的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文...