Detailed @RequestMapping property - SpringMVC master advanced

Foreword

In an interview in the daily java springMVC has been an indispensable part of, and experience sharing a moment yesterday with a few friends at the time of the interview SpringMVC framework.
We found that most interviewers only understood SpringMVC implementation process and components of this first layer, and some did say there is a kind of mold, but also some relatively fool, but when asked to comment SpringMVC (Annotation) specific implementation section, and more extremely embarrassed.

So I look at this record in one of the real problems we often examine the comments:

@RequestMapping annotation attributes are what? Respectively, they are used to do?

Imagine: If you answer this question, you have almost a certainty

RequestMapping Interface source code parsing

RequestMapping source interfaces are as follows, which defines eight attributes (Spring4.3.8).
Note: SpringMVC property in version 4.1 of RequestMapping been adjusted accordingly, remove the path attribute.

@Target({ElementType.METHOD, ElementType.TYPE}) // 可以在方法和类的声明中使用
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {

    String name() default "";// 指定映射的名称

    @AliasFor("path")
    String[] value() default {}; // 指定请求路径的地址

    @AliasFor("value")
    String[] path() default {}; // 指定请求路径的地址

	// 指定请求的方式,是一个RequsetMethod数组,可以配置多个方法
    RequestMethod[] method() default {};
	
	// 指定参数的类型
    String[] params() default {};
	
	// 指定请求头内容
    String[] headers() default {};
	
	// 指定数据请求的格式
    String[] consumes() default {};
	
	// 指定返回的内容类型
    String[] produces() default {};
}
  • As shown in the source code, there are two attributes @Target, respectively ElementType.METHOD and ElementType.TYPE, that is to say @RequestMapping method may be used in the declaration of class and

  • Properties of the annotation can be seen in addition to name () Returns a string, returns an array of all other methods, i.e. define a plurality of attribute values, for example value () and path () can be defined by a plurality of string values ​​simultaneously receiving a plurality of URL request

RequestMapping property description

1、name

Here name attribute corresponding to annotation method, a method easier to understand

@RequestMapping(value = "login",name = "用户登录")
@ResponseBody
public String login() {
	return "success";
}

The official said the document: it allows you to very easily use it on a JSP page, shaped like a static page like this can be called directly:

<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
...
<a href="${s:mvcUrl('PAC#getAddress').arg(0,'US').buildAndExpand('123')}">Get Address</a>

2、value

@Controller
@RequestMapping("user")   //此处如果不省略,则为@RequestMapping(value="user")
public class UserController {
 
    @RequestMapping("login")
    @ResponseBody
    public String login() {
		return "success";
	}
}

Specifying the requested physical address, the address may be specified URI template pattern (Template Pattern);

As the value of property is the default property @RequestMapping comment, so if there is only one property, you can omit the property name, if there is more than one property, you must write the name of the property value. That is the meaning of the following two marked as

@RequestMapping(value="login")
@RequestMapping("login")

value property supports wildcard matching:

@RequestMapping(value="login/*")

That is: http: // localhost: 8080 / login / 1 or http: // localhost: 8080 / login / hahaha able to properly access the Interface

Here through URL: http: // localhost: a visit by the login 8080 / user / login () method for processing

3、path

Is synonymous with the value, path (value) (path and reference value with each other, see RequestMapping source Interface)
path attribute, and an attribute value consistent, both of which are used as a map for use.

@RequestMapping (value = "login"), @ RequestMapping (path = "login"), both cases can be to login () method to access

path attribute supports wildcard matching:

@RequestMapping(path="login/*")

That is: http: // localhost: 8080 / login / 1 or http: // localhost: 8080 / login / abc can normally access

4、method

Specify the type of request, such as GET, POST, PUT, DELETE and the like;

@RequestMapping(value = "login",method = RequestMethod.GET)
@ResponseBody
public String login() {
	return "success";
}

The above method, said the method only supports GET requests.

Here also be

@RequestMapping(value = "login",method = {RequestMethod.GET,RequestMethod.POST})

The described method can simultaneously support GET and POST requests.

If there is no method attribute, then the method supports all HTTP requests.

5、params

When the property is specified, the request must include the predetermined parameters params attribute to perform the request

@RequestMapping(value = "login",params = "flag")
@ResponseBody
public String login() {
	return "success";
}

The method of the above described must be included in the request to execute the request flag parameter, the value of the parameter flag is not required

http: // localhost:? 8080 / login flag = xxx // normal access

http: // localhost: 8080 / login // inaccessible

@RequestMapping(value = "login",params = "flag=true")
@ResponseBody
public String login() {
	return "success";
}

The method of the above described flag parameters must be included in the request, and the parameter values ​​must be performed before the request is true

http: // localhost:? 8080 / login flag = true // normal access

http: // localhost:? 8080 / login flag = false // inaccessible

http: // localhost: 8080 / login // inaccessible

6、headers

  • Information for HTTP interaction is called collusion HTTP packet, HTTP packet sent by the client is referred to as request message, the server sends back to the client an HTTP response packet called packets, a packet header Department and message body composition.
  • Request headers (Request
    Headers): request header contains a lot of information about the client environment and request message body, such as a browser supported language, server address of the request, the client operating system.
  • Response header (Rsponse Headers): response header also contains a lot of useful information, including a server type, date, and type of encoded content in response to, in response to the length of the content and the like.

This property is specified, the request header must contain certain specified value, it is possible to make the processing request method

Such as the Chrome browser:

By clicking F12 (enter developer mode) ----> Network ----> Name click on the page ----> Headers can see on the right, below is a computer in my head sample request:

Request Headers
    Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Accept-Encoding:gzip, deflate, sdch
    Accept-Language:zh-CN,zh;q=0.8
    Cache-Control:max-age=0
    Connection:keep-alive
    Cookie:JSESSIONID=210075B5E521CWE3CDE938076295A57A
    Host:localhost:8080
    Upgrade-Insecure-Requests:1
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93
// 表示只接收本机发来的请求
@RequestMapping(path = "/login", headers="Referer=http://localhost:8080")
public String login() {
	return "success";
}

The above method, header must satisfy the request contains the specified "Referer" request headers and values ​​"http: // localhost: 8080" when to perform the request

7、consumes

Specifying process submission type (Content-Type) request, for example: application / json, when text / html, allow the process to be able to process the request

@RequestMapping(value = "login",consumes = "application/json")
@ResponseBody
public String login() {
	return "success";
}

8、produces

Content type of the returned content type is the type of request must return request header (the Accept) contained

@RequestMapping(value = "login",produces = "application/json")
@ResponseBody
public String login() {
	return "success";
}

Further, it produces the encoding attribute can also specify return values

@RequestMapping(value = "login",produces = "application/json,charset=utf-8")

As above, the specified return utf-8 encoded

Published 85 original articles · won praise 471 · views 170 000 +

Guess you like

Origin blog.csdn.net/qq_39390545/article/details/105206988