Spring MVC Series (ii) @requestmapping and URL mappings

Source: Internet
Author: User
Tags bind json regular expression

@RequestMapping is an annotation used to process a request address mapping, can be annotated with a class, or annotated with a method on a class, an annotation with a class, a uniform identity representing the controller, usually a representation of a module, an annotation in a method, It means that a request is processed by that method.


@RequestMapping have several properties


One: Value property

Value represents the path identifier of the URI, such as

The corresponding path is IP: Port number/project name/mapping/test, corresponding to our example in the previous section is Localhost:8080/mapping/test, so that the URL will map to the processing of our test method


1) Note that such mapping paths are unique, such as the following

Package Org.study.lyncc.web.controller;

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

@Controller
@RequestMapping (value= "/mapping") public
class Requestmappingcontroller {
    
    @RequestMapping (value= "/test")
    Public Modelandview Test () {
        return new Modelandview ("test");
    }
    
    @RequestMapping (value= "/test") public
    Modelandview test1 () {
        return new Modelandview ("Test1");
    }
    
}
If this is the code, the URI of the method test and Test1 's mapping path is/mapping/test,spring MVC starts with an error.

It means that/mapping/test has been mapped to the method test to deal with, but your test1 URI is still/mapping/test, so the error, in fact, very good understanding, if the two method corresponds to the same URL so the real request came over, in the end is who to deal with.


2) There are several types of values that can be used for value

① a common specific value, such as/test

② a uniform variable, usually identified with a {} symbol, such as/{id}

③ regular expressions, usually identified with the {} symbol, for example {method:\\w+}

④ above several types of collections


① 's example is not an example, let's give an example of ②

Package Org.study.lyncc.web.controller;

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

@Controller
@RequestMapping (value= "/mapping") public
class Requestmappingcontroller {
    
    @RequestMapping (value= "/{id}")
    Public Modelandview test1 () {
        Modelandview Mav = new Modelandview ("test");
        return MAV;
    }
    
    
}
When our URLs are as follows, we can map them to

Localhost:8080/spring-mvc/mapping/1

Localhost:8080/spring-mvc/mapping/2

localhost:8080/spring-mvc/mapping/234

Localhost:8080/spring-mvc/mapping/12dsfsa2

can be accurately mapped to and processed by the Test1 method.


Examples of ③ regular expressions

Package Org.study.lyncc.web.controller;

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

@Controller
@RequestMapping (value= "/mapping") public
class Requestmappingcontroller {
    
    @RequestMapping (value= "/{:\\d}")
    Public Modelandview Reg () {
        Modelandview Mav = new Modelandview ("test");
        return MAV;
    }
    
    
}
Corresponding to this we create a new test.jsp under the Web-inf/jsp folder

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
When we enter the following URL in the browser:

Http://localhost:8080/spring-mvc/mapping/1

Http://localhost:8080/spring-mvc/mapping/2

Http://localhost:8080/spring-mvc/mapping/3

Http://localhost:8080/spring-mvc/mapping/4

can be correctly redirected to

But if your URL is

http://localhost:8080/spring-mvc/mapping/d

Http://localhost:8080/spring-mvc/mapping/44

The regular expression "\d" is matched by a numeric character. equivalent to [0-9]. "D", "44" will not match the regular, so the method does not handle the URL


Examples of ④ synthesis

Package Org.study.lyncc.web.controller;

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

@Controller
@RequestMapping (value= "/mapping") public
class Requestmappingcontroller {
    
    @RequestMapping (value= "/{:\\d}/user/{id}/detail")
    Public Modelandview Comprehensivedemo () {
        Modelandview Mav = new Modelandview ("test");
        return MAV;
    }
    
    
}
Then the URL can be entered:
Http://localhost:8080/spring-mvc/mapping/4/user/abc/detail

This can still be mapped to


Two: Method property

The value of method is used to handle the properties of the user request, whether it is a GET request, a POST request, or a put request, and so on, spring MVC can be fine-grained management, because the value of mehtod is fixed, so spring MVC is managed with enumerations

Package org.springframework.web.bind.annotation;

public enum Requestmethod {

	GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE

}

Let's give an example to prove that:

Package Org.study.lyncc.web.controller;

Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping (value= "/mapping") public
class Requestmappingcontroller {
    
    @ Requestmapping (value = "/detail", method = requestmethod.get) public
    Modelandview Methodtestdemo () {
        Modelandview Mav = new Modelandview ("test");
        return MAV;
    }
    
    
}
The browser URL is http://localhost:8080/spring-mvc/mapping/detail because the browser defaults to a GET request

We modify the code to change the request to post

Package Org.study.lyncc.web.controller;

Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping (value= "/mapping") public
class Requestmappingcontroller {
    
    @RequestMapping (value = "/detail", method = Requestmethod.post)
    Public Modelandview Methodtestdemo () {
        Modelandview Mav = new Modelandview ("test");
        return MAV;
    }
    
    
}
And we again the same URL request

Prompt does not support get requests


Three: Headers

headers specifies that certain header values must be included in the request in order for the method to handle requests, which we usually don't normally use, let's do an example.

Package Org.study.lyncc.web.controller;

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

@Controller
@RequestMapping (value= "/mapping") public
class Requestmappingcontroller {
    
    @RequestMapping (value = "/detail", headers= "location=www.tuniu.com")
    Public Modelandview Headersdemo () {
        Modelandview Mav = new Modelandview ("test");
        return MAV;
    }
    
    
}
This way we specify that headers in the request need to have Key/value key value pair, key is Location,value is www.tuniu.com

We use a normal browser to access


Note that spring does not process, we use a third-party tool to test the restclient

When we join the request headers, we find that we can return the prompt 200 normally and return the body


Four: Consumer indicates the type of submission (Content-type) that specifies the processing request, such as Application/json, text/html, and so on

Let's just give it an example.

Package Org.study.lyncc.web.controller;

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

@Controller
@RequestMapping (value= "/mapping") public
class Requestmappingcontroller {
    
    @RequestMapping (value = "/detail", consumes= "Application/json")
    Public Modelandview Headersdemo () {
        Modelandview Mav = new Modelandview ("test");
        return MAV;
    }
    
    
}
Indicates that we request body Content-type: "Application\json;charset=utf-8" will respond if our browser directly accesses



Error, 415 errors, 415 error refers to the method of the current request and the requested resource, the entity submitted in the request is not a supported format in the server, so the request is denied


Well, we use tools to access

Request succeeded


V: produces specifies the type of content returned, only if the type in the request header (Accept) contains the specified type.

To illustrate:

Package Org.study.lyncc.web.controller;

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

@Controller
@RequestMapping (value= "/mapping") public
class Requestmappingcontroller {
    
    @RequestMapping (value = "/detail", produces= "Application/json")
    Public Object Headersdemo () {
        ...
    }
    
    
}
This generally returns Application\json format data, which will be analyzed later


All right, so far, about @requestmapping. Some common URL mappings have been explained to the end of the ~end~

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.