Some injection tags in Spring MVC

Source: Internet
Author: User

1. @Controller
@Controller is responsible for registering a bean into the spring context, the bean ID defaults to the first letter of the class name lowercase, you can also specify as follows: Method one:
@Controller
public class TestController {}

Method Two:
@Controller ("Tmpcontroller")
public class TestController {}
2. @RequestMapping

Use the @RequestMapping comment to annotate the function, which handles some HTTP methods, URIs, or HTTP headers. This comment is key to Spring REST support. You can change the method parameter to handle other HTTP methods.
For example:
@RequestMapping (Method=requestmethod.get, value= "/emps", headers= "Accept=application/xml, Application/json")

Headers only accept Application/xml in the request header, Application/json such a matching string

 以下是一次请求的报文头 headers 类容

      1. accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
      2. accept-charset:gbk,utf-8;q=0.7,*;q=0.3
      3. Accept-encoding:gzip,deflate,sdch
      4. accept-language:zh-cn,zh;q=0.8
      5. Cache-control:max-age=0
      6. Connection:keep-alive
      7. host:127.0.0.1:8080
      8. user-agent:mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.22 (khtml, like Gecko) chrome/25.0.1364.152 safari/537.22

@RequestMapping new parameters consumes and produces

The parameters of a header in the @requestmapping parameter are described earlier to specify the header content of the HTTP request request that handler method can accept.
Consumes and produces, however, go further by specifying the content type of request requests that can be accepted or produced directly.
For example

Java code
    1. @RequestMapping (value= "/testmsgconverter", consumes= "Text/plain", produces= "Application/json")
Indicates that the Content-type in the header of the request received by Handlermethod is Text/plain;
Accept for Application/json  3. Validation for @RequestBody

@RequestBody now directly supports @valid labeling, if validation fails, will throw
Requestbodynotvalidexception.
The specific processing logic is visible in the following code in Requestresponsebodymethodprocessor in spring.

Java Code Public Object resolveargument (methodparameter parameter,Modelandviewcontainer Mavcontainer,Nativewebrequest WebRequest,Webdatabinderfactory binderfactory) throws Exception {
Object arg = readwithmessageconverters (webRequest, parameter, Parameter.getparametertype ());
if (shouldvalidate (parameter, arg)) {
String argname = conventions.getvariablenameforparameter (parameter);
Webdatabinder Binder = Binderfactory.createbinder (WebRequest, ARG, argname);
Binder.validate ();
Errors Errors = Binder.getbindingresult ();
if (Errors.haserrors ()) {
throw new Requestbodynotvalidexception (errors);
}
}
return arg;
}

4. @PathVariable

Use @PathVariable annotations to insert a path variable in a URI as a parameter.
For example:
@RequestMapping (Method=requestmethod.get, value= "/emp/{id}")
Public Modelandview GetEmployee (@PathVariable String ID) {...}

Other useful notes
Use the @RequestParam to insert the URL parameter into the method.
Use a @RequestHeader to insert an HTTP header into the method.
Use the @RequestBody to insert the HTTP request body into the method.
Use @ResponseBody to return content or objects as HTTP response bodies.
Use httpentity<t> to insert it into the method automatically if it is supplied as an argument.
Returns an HTTP response with a custom state or header using Responseentity<t>.
For example:
Public @ResponseBody Employee Getemployeeby (@RequestParam ("name")
String name, @RequestHeader ("accept") string Accept, @RequestBody string body) {...}
Public Responseentity<string> method (httpentity<string> entity) {...}

Some injection tags in Spring MVC

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.