@RequestMapping

Personally, I suggest that you practice the construction process again. If you feel troublesome, you can directly copy the previous project, but you need to modify a bit of information in POM.xml

<groupId>com.hanpang.springmvc</groupId>
<artifactId>springmvc-demo02</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
Copy the code

The content of this chapter is good to understand briefly

1. Request parameters and request header values

Conditions can be filtered for request parameters to narrow the request match, such as “myParam”, “! “MyParam” and “myParam=myValue” etc. The first two criteria filter requests with/without certain request parameters, and the third criteria filter requests with specific parameter values. The following example shows how to use filter criteria for request parameter values:

Request parameter params

package com.hanpang.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller// Tell it is a controller
@RequestMapping("/book")
public class Demo04Controller {
	@RequestMapping(value="/xiyouji",params=! "" myParam" )
	public ModelAndView test01(a) {
		System.out.println("Name of parameter myParam not allowed");
		return null;
	}

	@RequestMapping(value="/shuihu",params="myParam")
	public ModelAndView test02(a) {
		System.out.println("The parameter passed by the path must have the name myParam.");
		return null;
	}
	@RequestMapping(value="/sanguo",params="myParam=shxt")
	public ModelAndView test03(a) {
		System.out.println("The parameter passed by the path must have the name myParam and the value passed must be SHXT.");
		return null;
	}
	@RequestMapping(value="/honglou",params="myParam! =shxt")
	public ModelAndView test04(a) {
		System.out.println("The parameter passed by the path must have the name myParam, and the value passed cannot be SHXT.");
		return null; }}Copy the code

NOTE: I rarely use the params attribute in my actual development. See the output statement in the code for an illustration

Request header headers

package com.hanpang.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller// Tell it is a controller
@RequestMapping("/book")
public class Demo04Controller {
	@RequestMapping(value="/sanguo",headers="myParam=shxt")
	public ModelAndView test03(a) {
		System.out.println("Headers needs to pass myParam and the value must be SHXT.");
		return null;
	}
	@RequestMapping(value="/honglou",headers="myParam! =shxt")
	public ModelAndView test04(a) {
		System.out.println("Headers needs to pass myParam and it can't be SHXT.");
		return null; }}Copy the code

This is a simple test using PostMan

The access path has no error, but the request header is not set. Error 404 is reported

The setup header is now accessible, but if the value is not SHXT, error 404 will still be reported

2. The type of media consumed

You can specify a set of media types that can be consumed to narrow down the mapping. The request will only be matched if the content-Type value in the request header is the same as the specified media Type that can be consumed. For example :(official code)

@Controller
@RequestMapping(path = "/pets", consumes="application/json")
public void addPet(@RequestBody Pet pet, Model model) {
    / /...
}
Copy the code

We will cover this later in the course, when the client passes data to the server, it is usually used for the client to pass JSON data

3. Type of media produced

You can specify a set of production media types to narrow down the mapping. The request will only be matched if the Accept value in the request header is the same as that in the specified producable media type. Furthermore, using the Produces condition ensures that the content used to generate the response is the same as the specified producable media type. For example :(official code)

@Controller
@RequestMapping(path = "/pets/{petId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {
    / /...
}
Copy the code

We’ll look at controller return types later, which we’ll explain

Note that the media type specified through the condition can also specify the character set. In the small piece of code above, for example, or overwrite the MappingJackson2HttpMessageConverter default configurations in the class of media types, at the same time also specifies the use of the utf-8 character set.

As is the consumes condition, negations can be used for expressions of the media types that are produced. For example, you can use! Text /plain matches all requests that do not contain text/plain in the request header Accept. There are also constants defined in the MediaType class, such as APPLICATION_JSON_VALUE and APPLICATION_JSON_UTF8_VALUE, which are recommended to be used more often.

The Produces attribute provides method-level type support. Unlike other attributes, when used at the type level, the method-level consumption type overrides the type-level configuration, not the inheritance relationship.