Spring MVC Supported Method Argument Annotations and Return Value

本文详细介绍了Spring MVC框架下不同类型的请求参数注解使用方法及返回值处理逻辑,包括RequestParam、RequestHeader、RequestBody、RequestPart等,并解释了ModelAttribute和PathVariable的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Supported Method Argument Annotations


 All these different method argument types and annotations allow us to write very flexible request handling methods.

 

RequestParam

The RequestParam annotation can be placed on any argument in a request-handling method. When

present, it is used to retrieve a parameter from the request. When put on a Map, there is some special

handling, depending on whether the name attribute is set. If the name is set, the value is retrieved and

converted into a Map. For conversion, if no name is given, all request parameters are added to the map as key/value pairs.

 

RequestHeader

The RequestHeader annotation can be placed on any method argument. It is used to bind a method

argument to a request header. When placed on a Map, all available request headers are put in the map as key/value pairs. If it is placed on another type of argument, then the value is converted into the type by using a org.springframework.core.convert.converter.Converter or PropertyEditor.

 

RequestBody

The RequestBody annotation is used to mark a method parameter we want to bind to the body of the web request. The body is converted into the method parameter type by locating and calling an

org.springframework.http.converter.HttpMessageConverter. This converter is selected based on the

requests content-type. If no converter is found, an

org.springframework.web.HttpMediaTypeNotSupportedException is thrown. By default, this leads to a

response with code 415 (SC_UNSUPPORTED_MEDIA_TYPE) being send to the client.

Optionally, method parameters can also be annotated with javax.validation.Valid or

org.springframework.validation.annotation.Validated to enforce validation for the created object.

 

RequestPart

When the RequestPart annotation is put on a method argument of the type javax.servlet.http.Part,

org.springframework.web.multipart.MultipartFile (or on a collection or array of the latter,) then we

will get the content of that file (or group of files) injected. If it is put on any other argument type, the

content is passed through an org.springframework.http.converter.HttpMessageConverter for the

content type detected on the file. If no suitable converter is found, then an

org.springframework.web.HttpMediaTypeNotSupportedException is thrown.

 

ModelAttribute

The ModelAttribute annotation can be placed on method arguments, as well as on methods. When

placed on a method argument, it is used to bind this argument to a model object. When placed on a

method, that method is used to construct a model object, and this method will be called before request handling methods are called. These kinds of methods can be used to create an object to be edited in a form or to supply data needed by a form to render itself.

 

PathVariable

The PathVariable annotation can be used in conjunction with path variables. Path variables can be used in a URL pattern to bind the URL to a variable. Path variables are denoted as {name}in our URL mapping. If we were to use a URL mapping of /book/{isbn}/image, then isbn would be available as a path variable.

 

CookieValue

This CookieValue annotation can be placed on any argument in the request-handling method. When

present, it is used to retrieve a cookie. When placed on an argument of type javax.servlet.http.Cookie,

we get the complete cookie. Otherwise, the value of the cookie is converted into the argument type.

 

 Supported Method Return Values



 When an arbitrary object is returned and there is no ModelAttribute annotation present, the

framework tries to determine a name to use as the name for the object in the model. It basically takes the simple name of the class (the classname without the package) and lowercases the first letter. For

example, the name of our com.apress.prospringmvc.bookstore.domain.Book becomes book. When the

return type is a collection or array, it becomes the simple name of the class, suffixed with List. Thus a

collection of Book objects becomes bookList.

This same logic is applied when we use a Model or ModelMap to add objects without an explicit name.

This also has the advantage of using the specific objects, instead of a plain Map to gain access to the

underlying implicit model.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值