Summary of how to write annotation arguments

Introduction

As I wrote in another article, I didn't touch Java very much during the heyday of annotation (?), So I didn't understand the argument rules of annotation well, and even if I googled, there was nothing that met the demand, so I will write it myself. For those who have the same question in the future.

Below, the annotation shown in the example uses @RequestMapping.

Term definition

It may not be accurate, but I use it in this sense below.

Arguments
The name that associates the value you specify when writing the annotation.
Element
The name that the annotation accepts in the reference.

Rule 1: Those with default values can be omitted

Looking at the reference of @RequestMapping, some elements are written, but since the default values are all specified, you can write as follows. The default value for each element is used.

@RequestMapping

Rule 2: Can be omitted if the argument name is value

The following have the same meaning

@RequestMapping(value = "/")
@RequestMapping("/")

In the case of Spring, @AliasFor is prepared, and the place where" What does the value of Mapping mean? "Can be written as path. See the reference for which element is an alias for value. I think that something that is almost as perceived is another name.

@RequestMapping(path = "/")

Rule 3: Arguments may be written as scalar values even if the element type is an array

The compiler extends it to an array.

// value -> {"/"}
@RequestMapping("/")

You may or may not write your own {}. However, {} is required when writing two or more arguments as explained below.

// value -> {"/", "/index"}
@RequestMapping("/", "/index")
// value -> {"/", "/index"}
@RequestMapping({"/", "/index"})

Rule 4: Argument name required if there are two or more arguments

It was hard to understand because I thought it was like a required argument and an optional argument like Python's default value argument.

@RequestMapping(value = "/", method = RequestMethod.GET)

//I want to decorate Python I can't write as follows
@RequestMapping("/", method = RequestMethod.GET)

Recommended Posts

Summary of how to write annotation arguments
Summary of how to implement default arguments in Java
How to write easy-to-understand code [Summary 3]
[java] Summary of how to handle char
[Java] [Maven3] Summary of how to use Maven3
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
Summary of how to select elements in Selenium
JDBC promises and examples of how to write
Summary of how to create JSF self-made tags
[Java] Summary of how to abbreviate lambda expressions
How to write good code
How to write Scala from the perspective of Java
[Java] Types of comments and how to write them
Bit Tetris (how to write)
How to write java comments
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
Summary of Java communication API (1) How to use Socket
How to write Junit 5 organized
Summary of Java communication API (3) How to use SocketChannel
How to write Rails validation
How to write Rails seed
Summary of Java communication API (2) How to use HttpUrlConnection
[Ruby] How to write blocks
How to write Rails routing
Comparison of how to write Callback function (Java, JavaScript, Ruby)
Studying Java # 6 (How to write blocks)
[Rails] How to write in Japanese
Baseball ball count (how to write)
How to use setDefaultCloseOperation () of JFrame
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write Java variable declaration
How to use MyBatis Mapper annotation
Y-shaped road tour (how to write)
[RSpec] How to write test code
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
Basics of Java development ~ How to write programs (variables and types) ~
How to write ruby if in one line Summary by beginner
[Java] How to write when passing two or more arguments to super
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Rails] How to resolve wrong number of arguments (given 2, expected 0..1) of user_id
How to write offline real-time Java implementation example of F01 problem
How to make a Java calendar Summary
Summary of stumbling blocks related to form_with
How to name variables 7 selections of discomfort
[Basic] How to write a Dockerfile Self-learning ②
[Introduction to Java] How to write a Java program
[Java] How to output and write files!
How to determine the number of parallels
Summary of going to JJUG CCC 2019 Spring
How to micro-benchmark while changing VM arguments
How to sort the List of SelectItem
How to write Spring AOP pointcut specifier
How to write an RSpec controller test