java-common annotations

JDK comes with annotations

  • @Override overrides, identifies the method that overrides its parent class
  • @Deprecated has expired, indicating that the method is deprecated
  • @Suppvisewarnings suppress warnings, suppress warnings

meta-annotation

@Target    indicates where the annotation is used. The possible values ​​include:

  • ElemenetType.CONSTRUCTOR constructor declaration
  • ElemenetType.FIELD field declaration (including enum instances)
  • ElemenetType.LOCAL_VARIABLE local variable declaration
  • ElemenetType.METHOD method declaration
  • ElemenetType.PACKAGE package declaration
  • ElemenetType.PARAMETER parameter declaration
  • ElemenetType.TYPE class, interface (including annotation types) or enum declaration
  • ElementType.ANNOTATION_TYPE     注解

@Retention    indicates at what level the annotation information is saved. Optional RetentionPolicy parameters include:

  • The RetentionPolicy.SOURCE annotation will be discarded by the compiler
  • The RetentionPolicy.CLASS annotation is available in the class file, but is discarded by the VM
  • RetentionPolicy.RUNTIME The JVM will also retain annotations during runtime, so the annotation information can be read through the reflection mechanism.

@Documented   includes this annotation in the javadoc

@Inherited    allows subclasses to inherit annotations from the parent class

Common third-party annotations

@Repository: used to mark data access components, ie DAO components

@Service: used to mark business layer components

@Transactional: Declare that all methods of this service require transaction management. A transaction is opened at the beginning of each business method

@Controller: control layer

@Component: Give the neutral class to spring management

@Autowired: Autowire, automatically inject the value from the bean container into the bean

@Path: process REST request, interface path

@Method:    

There are five commonly used HTTP verbs (the corresponding SQL commands are in parentheses).

  • GET (SELECT): Retrieve resource(s) from the server.
  • POST (CREATE): Create a new resource on the server.
  • PUT (UPDATE): Update the resource on the server (the client provides the complete resource after the change).
  • PATCH (UPDATE): Update the resource on the server (client provides changed properties).
  • DELETE (DELETE): Deletes the resource from the server.

There are also two less commonly used HTTP verbs.

  • HEAD: Get metadata of the resource.
  • OPTIONS: Get information about which properties of the resource can be changed by the client.

@Accept和@Content-Type

@Accept: It means the data format that the interface should return to the client
@Content-Type: Indicates the data format sent by the client to the server. This is defined when writing the REST interface 
   Normally, if the server does not define Accept but adds it by itself, it will report 404, and the corresponding interface is not found.
 
@Produces    indicates the MIME data type returned by the class or method

There are several formats as follows:

(1) @Produces("text/plain") text type

(2)@Produces("text/html")  Html类型

(3) @Produces({"application/xml"}) Xml type

(4)@Produces({ "application/json"}) Json类型

       Two or more MIME types can be annotated at one time. The format is: {"application/xml", "application/json"} This means that both can be used, but the former is generally selected when choosing, namely application/xml, Because it appeared for the first time.

@Consumes     represents the MIME types that a resource can accept
 
@Queryparam vs @Pathparam

@Queryparam: Specifies that the parameters in the URL appear in the form of key-value pairs, while in the program @QueryParam("from") int from reads the value of from in the URL, 

For example: URL input is: users?from=100&to=200&orderBy=age&orderBy=name 

@Pathparam: Only the value of the parameter appears in the URL, not the key-value pair

For example: /users/100 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324478853&siteId=291194637