A dot eyeball.

Common Spring MVC annotations include the following:

1. @Controller

The @controller annotation on the class indicates that the class is a Controller in Spring MVC. Declare it as a Spring Bean. The DispatcherServlet will automatically scan the class annotated with this annotation. And map requests to @requestMapping annotated methods. Specifically, @Component, @service, @repository and @Controller are the same when declaring normal beans, because @Controller, @Service and @Repository both combine the @Component meta-annotation, but only @Controller can be used when Spring MVC declares Controller beans.

2. @RequestMapping

The @RequestMapping annotation is used to map Web requests (access paths and parameters), processing classes and methods. RequestMapping can be annotated on a class or method. The @requestMapping path in the annotation method inherits the annotation path in the class. @RequestMapping supports request and Response as parameters in the Servlet and also supports the configuration of request and response media types.

3.@ResponseBody

@responseBody supports putting the return value in the response body instead of returning a page. In many Ajax-based applications, you can use this annotation to return data instead of pages; This annotation can be placed before a return value or on a method.

4. @RequestBody

RequestBod allows the parameters of a request to reside in the request body, rather than being linked directly to the address. This annotation is placed before the parameter.

5. @PathVariable

@pathvariable is used to accept path parameters, such as /news/001, which can accept 001 as a parameter. This annotation is placed before the parameter.

6.@RestController

@RestController is a composite annotation that combines @Controller and @responseBody, which means you need to use this annotation when you’re only developing a control that interacts with data on the page. If you don’t have this annotation, you’ll need to add @Controller and @responseBody annotations to your code.

Example 2.

1. The value classes

Add Jackson and related dependencies to get transformations between objects and JSON and XML

<! - support for json and XML format - > < the dependency > < groupId > com. Fasterxml. Jackson. Dataformat < / groupId > < < artifactId > Jackson - dataformat - XML/artifactId > < version > 2.5.3 < / version > < / dependency >Copy the code

In particular, in a real project, we support JSON data primarily, and there is no need to support both JSON and XML because JSON is much more compact than XML. Json is the most recommended format due to the widespread use of JavaScript, in which case our dependency package is as follows (the dependencies above contain the dependencies below) :

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> The < version > 2.5.3 < / version > < / dependency >Copy the code

This class is used to demonstrate getting the request object parameters and returning the object to Response:

package org.light4j.springMvc4.domain; public class DemoObj { private Long id; private String name; Public DemoObj() {//① super(); } public DemoObj(Long id, String name) { super(); this.id = id; this.name = name; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}Copy the code

Code explanation:

Jackson must use this empty construct when converting objects to JSON.

2. Annotation demo controller

package org.light4j.springMvc4.web; import javax.servlet.http.HttpServletRequest; import org.light4j.springMvc4.domain.DemoObj; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @controller // ① @requestMapping ("/anno") //② public class DemoAnnoController {@requestmapping (Produces = "text/plain; Public @responseBody String index(HttpServletRequest Request) {// ④ return "url:" + request.getRequestURL() + " can access"; } @RequestMapping(value = "/pathvar/{str}", produces = "text/plain; ⑤ public @responsebody String demopath (@pathvariable String STR, //③ HttpServletRequest request) {return "url:" + request.getrequesturl () + "can access, STR :" + STR; } @RequestMapping(value = "/requestParam", produces = "text/plain; Public @responseBody String passRequestParam(); HttpServletRequest request) { return "url:" + request.getRequestURL() + " can access,id: " + id; } @RequestMapping(value = "/obj", produces = "application/json; //⑦ @responseBody // ⑧ public String passObj(DemoObj obj, HttpServletRequest request) { return "url:" + request.getRequestURL() + " can access, obj id: " + obj.getId()+" obj name:" + obj.getName(); } @RequestMapping(value = { "/name1", "/name2" }, produces = "text/plain; //⑨ public @responseBody String remove(HttpServletRequest Request) {return "url:" + request.getRequestURL() + " can access"; }}Copy the code

Code explanation:

② @requestMapping (“/anno”) maps the access path of this class to /anno. (3) This method does not mark the path, so use the class-level path /anno; Produces produces the returned response media type and character set, or json object, set Porduces =”application/json; Charset =UTF-8″, which will be demonstrated later. The demo accepts HttpServletRequest as a parameter, and of course accepts HttpServletResponse as a parameter. Here @responseBody is used before the return value. ⑤ Demo accepts path parameters and uses @pathvariable before method parameters. The access path is /anno/pathvar/xxx. /anno/requestParam? Id = 1. Parameter to object, access path is /anno/obj? Id = 1 & name = xx. ⑧ @responsebody can also be used as a method. ⑨ demonstrates mapping different paths to the same method with access paths /anno/name1 or /anno/name2.

3. @ RestController demonstration

package org.light4j.springMvc4.web; import org.light4j.springMvc4.domain.DemoObj; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @restController // ① @requestMapping ("/rest") public class DemoRestController {@requestMapping (value = value) "/getjson",produces={"application/json; Charset = utF-8 "}) // ② public DemoObj getjson (DemoObj obj){return new DemoObj(obj.getid ()+1, obj.getName()+"yy"); //③} @requestMapping (value = "/getxml", produces={"application/ XML; Charset = utF-8 "})//④ public DemoObj getxml(DemoObj obj){return new DemoObj(obj.getid ()+1, obj.getName()+"yy"); }}Copy the code

Code explanation:

① Use @restController to declare the controller and return data without @responseBody. ② The media type of the returned data is JSON. ③ Return the object directly, and the object will be automatically converted to JSON. ④ The media type of the returned data is XML. ⑤ Return the object directly, and the object will be converted to XML automatically.

The following figure shows the return JSON result:



The result of the XML run is as follows:

Three. Source code examples:

Github Address: Click to view code cloud address: click to view

exceptional
Welcome to follow the life designer’s wechat public account



longjiazuoA