Java Web Common Annotations
Java SE
// programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. |
Spring Projects
Spring
Bean declaration
Make a bean belong to a particular profile
// Only can contains one !string. Can't be like {"!dev","!test"} |
Bean injection
Specify injected bean name
Bean initMethod and destroyMethod
|
|
Configurations
Configuration file properties
@PropertySource
|
@ConfigurationProperties
|
|
Spring MVC
Request
Request Information
|
Response
Convert Request Parameter Types
/** |
|
Data Validation
@Valid |
Data Validation Constraints Annotations
See JavaEE Validation Constraints Annotations
Exception Handling
Spring Data Access
By default all RuntimeException
s rollback transaction whereas checked exceptions don’t. This is an EJB legacy. You can configure this by using rollbackFor()
and noRollbackFor()
annotation parameters: @Transactional(rollbackFor=Exception.class)
. This will rollback transaction after throwing any exception.
Programatically and manually roll back
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); |
Spring Security
Spring Boot
Spring boot basic configuration
- @SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiguration
- @Configuration: This annotation marks a class as a Configuration class for Java-based configuration. This is particularly important if you favor Java-based configuration over XML configuration.
- @ComponentScan: This annotation enables component-scanning so that the web controller classes and other components you create will be automatically discovered and registered as beans in Spring’s Application Context.
- @EnableAutoConfiguration: This annotation enables the magical auto-configuration feature of Spring Boot, which can automatically configure a lot of stuff for you.
Scan MyBatis mappers
For example
Wildcard
*
: wildcard for one directory**
: wildcard for multiple level directory path
Java EE
Validation Constraints
validate object or string
// a constrained CharSequence, Collection, Map, or Array is valid as long as it's not null, but it can be empty |
validate number
validate datetime
// The annotated element must be an instant, date or time in the future. |
For more details refer to Java EE 8 javax.validation.constraints annotations
Jackson
JSON Serialization
Update format of serialized JSON value from Date type
|
Update field name of serialized JSON
|
Ignore properties for JSON serialization
// for field |
Serialize Enum to JSON String
|
Property documentation, metadata
@JsonPropertyDescription |
JSON Deserialization
Ignore Unknown Fields
@JsonIgnoreProperties(ignoreUnknown = true) |
Convert JSON String to Date
|
|
Convert JSON String to Enum field (Note: The @JsonValue
only deserialize for @RequestBody
fields)
|
For more details refer to Jackson Annotations
Hibernate
MyBatis-Plus
Specify Table name
Specify primary key
Specify field is not as a column of table of database
When field name same with keywords of database need use backquote to avoid SQL execution error
|
Logic delete
|
For more details refer to MyBatis-Plus Annotations
Lombok
POJO
|
@Data
: A shortcut for@ToString
,@EqualsAndHashCode
,@Getter
on all fields, and@Setter
on all non-final fields, and@RequiredArgsConstructor
.
Generate a null-check statement
public NonNullExample( { Person person) |
Result code
public NonNullExample( { Person person) |
Automatic resource management: Call your close() methods safely with no hassle.
public static void main(String[] args) throws IOException { |
To sneakily throw checked exceptions without actually declaring this in your method’s throws clause
|
Result code
public String utf8ToString(byte[] bytes) { |
Annotate any class with a log annotation to let lombok generate a logger field.
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName()); |
Others
For more details refer to Lombok features.
You can rolling Lombok Back with Delombok tool.