Binding Annotations

All of the binding annotations support customization of the name of the variable being bound from with their name member.

The following table summarizes the annotations, their purpose and provides an example:

Table 1. Parameter Binding Annotations
AnnotationDescriptionExample

@Body

Binds from the body of the request

@Body String body

@CookieValue

Binds a parameter from a cookie

@CookieValue String myCookie

@Header

Binds a parameter from an HTTP header

@Header String contentType

@QueryValue

Binds from a request query parameter

@QueryValue String myParam

@Part

Binds from a part of a multipart request

@Part CompletedFileUpload file

@RequestAttribute

Binds from an attribute of the request. Attributes are typically created in filters

@RequestAttribute String myAttribute

@PathVariable

Binds from the path of the request

@PathVariable String id

@RequestBean

Binds any Bindable value to single Bean object

@RequestBean MyBean bean

When a value is not specified to any binding annotation then the parameter name is used. In other words the following two methods are equivalent and both bind from a cookie called myCookie:

  1. @Get("/cookieName")
  2. public String cookieName(@CookieValue("myCookie") String myCookie) {
  3. // ...
  4. }
  5. @Get("/cookieInferred")
  6. public String cookieInferred(@CookieValue String myCookie) {
  7. // ...
  8. }
  1. @Get("/cookieName")
  2. String cookieName(@CookieValue("myCookie") String myCookie) {
  3. // ...
  4. }
  5. @Get("/cookieInferred")
  6. String cookieInferred(@CookieValue String myCookie) {
  7. // ...
  8. }
  1. @Get("/cookieName")
  2. fun cookieName(@CookieValue("myCookie") myCookie: String): String {
  3. // ...
  4. }
  5. @Get("/cookieInferred")
  6. fun cookieInferred(@CookieValue myCookie: String): String {
  7. // ...
  8. }

Because hyphens are not allowed in variable names it may be necessary to set the name in the annotation. The following two definitions are equivalent:

  1. @Get("/headerName")
  2. public String headerName(@Header("Content-Type") String contentType) {
  3. // ...
  4. }
  5. @Get("/headerInferred")
  6. public String headerInferred(@Header String contentType) {
  7. // ...
  8. }
  1. @Get("/headerName")
  2. String headerName(@Header("Content-Type") String contentType) {
  3. // ...
  4. }
  5. @Get("/headerInferred")
  6. String headerInferred(@Header String contentType) {
  7. // ...
  8. }
  1. @Get("/headerName")
  2. fun headerName(@Header("Content-Type") contentType: String): String {
  3. // ...
  4. }
  5. @Get("/headerInferred")
  6. fun headerInferred(@Header contentType: String): String {
  7. // ...
  8. }