Spring boot annotations

Source: Internet
Author: User

@SpringBootApplication: Contains the following:

[Email protected]

[Email protected]

[Email protected]

@ComponentScan component scans to automatically discover and assemble some beans, scanning to classes that have @component, @Controller, @Service, and so on

@Configuration an XML configuration file that is equivalent to spring and can be checked for type safety using Java code.

@EnableAutoConfiguration automatic Configuration

@Component can be used with Commandlinerunner to perform some basic tasks after the program starts.

@RestController Annotations are collections of @controller and @responsebody, which means that this is a controller bean, and that it is a rest-style controller that fills the return value of the function directly into the HTTP response body.

@Autowired Auto-Import.

@PathVariable get parameters.

@ResponseBody: Indicates that the return result of the method is written directly to the HTTP response body, which is generally used when fetching data asynchronously for building restful APIs.

After using @requestmapping, the return value usually resolves to the jump path, plus @responsebody returns the result will not be resolved to the jump path, but is written directly to the HTTP response body. For example, asynchronously fetching JSON data, plus @responsebody, will return the JSON data directly. This annotation is generally used in conjunction with @requestmapping. Example code:

@RequestMapping ("/publicreturn" OK ";}

@Controller: Used to define the controller class, which is the responsibility of the controller in the spring project to forward the URL request from the user to the corresponding service interface (services layer), generally this annotation is in the class, The usual method needs to be matched with the annotation @requestmapping, which can be substituted with @restcontroller @controller

Example code:

@Controller @RequestMapping ("/private  demoinfoservice demoinfoservice;@ Requestmapping ("/hello") public String Hello (map<string,object> Map) {   System.out.println ("Democontroller.hello ()");   Map.put ("Hello", "from templatecontroller.hellohtml");    // the rendering is displayed using Hello.html or HELLO.FTL templates.   return"/hello";}}

@RestController: Used to label control-layer components, such as action in Struts, @ResponseBody and @controller collections.

@RequestMapping: Provides routing information that is responsible for mapping the URL to a specific function in the controller.

@RequestParam: Gets the value in the URL parameter

   @RequestMapping (value = "/loginbypost", method = requestmethod.post)      public String loginbypost ( @RequestParam true ) string name,              @RequestParamtrue) string pwd) {          System.out.println ( "Hello post");           return Login4return (name, pwd);      }  

@RequestBody

    /*** The request content is a JSON string, spring will automatically match him to our parameter bean, but add @requestbody annotation *@paramname *@parampwd *@return      */@RequestMapping (Value= "/loginbypost2", method ={requestmethod.post, requestmethod.get}) PublicString loginByPost2 (@RequestBody requestloginbean loginbean) {if(NULL!=Loginbean) {              returnLogin4return (Loginbean.getname (), loginbean.getpwd ()); } Else {              return"Error"; }      }  

@Autowired: Automatic import of dependent beans, when added (Required=false), even if the bean can not find the error.

@Resource (name= "name", type= "type"): The default byname if there is no bracketed content. A similar thing to @autowired do.

@Service: Components typically used to decorate the Service layer

@Repository: Use @repository annotations to ensure that DAO or repositories provides exception translation, and that this annotation-decorated DAO or repositories class is Componetscan discovered and configured, There is also no need to provide them with XML configuration items.

@Bean: The @bean annotation method is equivalent to the Bean configured in XML.

@Value: The value of the property injected into the spring boot application.properties configuration. Example code:

@Value (Value =private String message;

@Component: Used to declare a class as a component class for persistence, and can then be obtained using autowired

@Bean: The equivalent of XML, placed above the method, rather than the class, meaning to produce a Bean and give it to spring management.

Params: Specifies that some parameter values must be included in the request before the method is processed.
Headers: Specifies that certain header values must be included in the request in order for the method to process requests.
Value: Specifies the actual address of the request, the specified address can be URI Template mode
Method: Specifies the type of method requested, GET, POST, PUT, delete, etc.
Consumes: Specifies the type of submission to process the request (Content-type), such as application/json,text/html;
Produces: Specifies the type of content returned, only if the type in the request header (Accept) contains the specified type to return

@PathVariable: Path variable, gets the value of the variable in the path. Such as

Requestmapping ("user/get/mac/public//

Spring boot annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.