Home  >  Article  >  What are springmvc annotations?

What are springmvc annotations?

anonymity
anonymityOriginal
2019-05-06 14:43:5615088browse

SpringMVC is an MVC framework similar to Struts2. In actual development, it receives the browser's request response, processes the data, and then returns the page for display, but it is much easier to get started than Struts2. . And due to the security issues exposed by Struts2, SpringMVC has become the preferred framework for most enterprises.

What are springmvc annotations?

There are many encapsulated annotations in springMVC. Using these annotations can simplify our development. The following are some commonly used annotations:

1. @Controller
@Controller is used to mark a class, and the class marked with it is a SpringMVCController object. The dispatch processor will scan the method of the class using this annotation and detect whether the method is annotated with @RequestMapping. @Controller just defines a controller class, and the method annotated with @RequestMapping is the processor that actually handles the request. Simply using the @Controller mark on a class cannot truly say that it is a controller class of SpringMVC, because Spring does not recognize it at this time. So how can Spring recognize it? At this time, we need to hand over this controller class to Spring for management. There are two ways:
 (1) Define the bean object of MyController in the SpringMVC configuration file.
 (2) In the SpringMVC configuration file, tell Spring where to find the Controller marked @Controller.

2. @RequestMapping
The RequestMapping annotation has six attributes. Below we divide it into three categories for explanation
(1)value, method
value: specify The actual address of the request, the specified address can be URI Template mode;
method: Specify the method type of the request, GET, POST, PUT, DELETE, etc.;
value uri value can be the following three categories: ordinary specific Value; a type of value containing a certain variable; a type of value containing a regular expression;

(2)consumes, produces
consumes: Specifies the submission content type (Content-Type) for processing the request, for example application/json, text/html;
produces: Specifies the content type to be returned. It will be returned only if the (Accept) type in the request header contains the specified type;

(3)params, headers
params: This method will only process the request if it must contain certain parameter values.
headers: The specified request must contain certain specified header values ​​in order for this method to process the request.

3.@Resource and @Autowired
@Resource and @Autowired are both used for bean injection. In fact, @Resource is not an annotation of Spring. Its package is javax. .annotation.Resource needs to be imported, but Spring supports the injection of this annotation, and both can be written on fields and setter methods. If both are written on the fields, then there is no need to write setter methods.

4.@PathVariable
is used to map the template variables in the request URL to the parameters of the function processing method, that is, take out the variables in the uri template as parameters.

5.@ResponseBody
Function: This annotation is used to convert the object returned by the Controller method into the specified format through the appropriate HttpMessageConverter, and then write it to the body of the Response object. data area.
Usage timing: Use it when the returned data is not a page with html tags, but data in some other format (such as json, xml, etc.)

6.@RestController
We often see some controllers implementing the REST API, just to serve JSON, XML or other custom type content. @RestController is used to create REST type controllers and @Controller types. @RestController is such a type, which prevents you from repeatedly writing @RequestMapping and @ResponseBody.

The above is the detailed content of What are springmvc annotations?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn