Home  >  Article  >  Java  >  How to map an http request to a method? Getting Started with SpringBoot: URL Mapping

How to map an http request to a method? Getting Started with SpringBoot: URL Mapping

php是最好的语言
php是最好的语言Original
2018-07-27 10:24:025756browse

0x000 Overview: Map an http request to a method. 0x001 @RequestMapping: This annotation can be added to a Controller or a method. If added to a Controller

0x000 Overview

Map a http request to On a certain method

0x001 @RequestMapping

This annotation can be added to a certain Controller or a certain method. If added to Controller, then all route mappings in this Controller will be added with this prefix (there will be chestnuts below). If it is added to the method, there will be no prefix (there will be chestnuts below).

@RequestMapping has the following attributes

  • value: The path of the requested URL

  • path: Same as value

  • method: Requested method

  • consumes: Allowed media types, that is, Content-Type

  • produces: The corresponding media type, that is, Accept

  • ##params: Request parameters

  • headers: Request headers

0x002 route matching

  1. First write

    Controller, Controller@RestController in the header marks this controller as RestController:

    package com.lyxxxx.rest.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
    }

  2. Add method, and add

    URL to match:

        @RequestMapping()
        public Object hello() {
            return "hello";
        }

    Description: The above adds a method named hello, which is not given Any attribute will match all URL by default, and the method is GET, so we can directly use the GET method to access the route

    $ curl 127.0.0.1:8080
    hello
    $ curl 127.0.0.1:8080/user
    hello
    $ curl 127.0.0.1:8080/user/1
    hello
  3. Exact match

    @RequestMapping(value = "/hello2")
    public Object hello2() {
        return "hello2";
    }

    Explanation: The above sets value to hello2, so access hello2will execute hello2method

    $ curl 127.0.0.1:8080/hello2
    hello2
  4. ##Character fuzzy matching
  5. @RequestMapping(value = "/hello3/*")
    public Object hello3() {
        return "hello3";
    }

    Note

    : The above value is set to /hello3/*, * is to match all characters, that is, access hello3 All URL will match this prevention<pre class="brush:php;toolbar:false">$ curl 127.0.0.1:8080/hello3/user hello3  curl 127.0.0.1:8080/hello3/1 hello3</pre>

  6. Single character fuzzy match
  7. $ curl 127.0.0.1:8080/hello4/1
    hello4

    Description

    : The above sets value to /hello4/?, ? matches a single character, and matches hello4/1, but it does not Matchhello4/11<pre class="brush:php;toolbar:false">$ curl 127.0.0.1:8080/hello4/1 hello4 $ curl 127.0.0.1:8080/hello4/12 {&quot;timestamp&quot;:&quot;2018-07-25T05:29:39.105+0000&quot;,&quot;status&quot;:404,&quot;error&quot;:&quot;Not Found&quot;,&quot;message&quot;:&quot;No message available&quot;,&quot;path&quot;:&quot;/hello4/12&quot;}</pre>

  8. Fuzzy matching of full path
  9. @RequestMapping(value = "/hello5/**")
    public Object hello5() {
        return "hello5";
    }

    Explanation

    : The above will value is set to /hello5/**, ** is to match all paths, so all routes below hello5 will match this Method<pre class="brush:php;toolbar:false">$ curl 127.0.0.1:8080/hello5 hello5 $ curl 127.0.0.1:8080/hello5/user/1 hello5</pre>

  10. Multiple path matching
  11. @RequestMapping(value = {"/hello6", "/hello6/1"})
    public Object hello6() {
        return "hello6";
    }

    $ curl 127.0.0.1:8080/hello6
    hello6
    $ curl 127.0.0.1:8080/hello6/1
    hello6F
  12. Read configuration
  13. // resources/application.properties
    
    app.name=hello7
    
    // com.lyxxxx.rest.controller.HelloController
    
    @RequestMapping(value = "${app.name}")
    public Object hello7() {
        return "hello7";
    }

    $ curl 127.0.0.1:8080/hello7
    hello7
  14. 0x003 method matching

matches the

method

in the request, the enumeration value written in RequestMethod:

  • GET

  • HEAD

  • POST

  • PUT

  • ##PATCH
  • DELETE
  • OPTIONS
  • TRACE
  • package com.lyxxxx.rest.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MethodController {
        @RequestMapping(path = "method/get", method = RequestMethod.GET)
        public Object get() {
            return "get";
        }
    
        @RequestMapping(path = "method/head", method = RequestMethod.HEAD)
        public Object head() {
            return "head";
        }
    
        @RequestMapping(path = "method/post", method = RequestMethod.POST)
        public Object post() {
            return "post";
        }
    
        @RequestMapping(path = "method/put", method = RequestMethod.PUT)
        public Object put() {
            return "put";
        }
    
        @RequestMapping(path = "method/patch", method = RequestMethod.PATCH)
        public Object patch() {
            return "patch";
        }
    
        @RequestMapping(path = "method/delete", method = RequestMethod.DELETE)
        public Object delete() {
            return "delete";
        }
    
        @RequestMapping(path = "method/options", method = RequestMethod.OPTIONS)
        public Object options() {
            return "options";
        }
    
        @RequestMapping(path = "method/trace", method = RequestMethod.TRACE)
        public Object trace() {
            return "trace";
        }
    
    
    }
    $ curl -X GET  127.0.0.1:8080/method/get
    get
    $ curl -X POST 127.0.0.1:8080/method/post
    post
    $ curl -X DELETE 127.0.0.1:8080/method/delete
    delete
    $ curl -X PUT 127.0.0.1:8080/method/put
    put
    ...

    0x003 params matching
In addition to matching

URL

and

method, it can also match params

package com.lyxxxx.rest.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ParamsController {
    @RequestMapping(path = "/params", params = "userId=1")
    public Object params() {
        return "params";
    }
}
$ curl 127.0.0.1:8080/params?userId=1
params
0x004 Description

The above reference data: "The essence of Spring Boot2 from building a small system to architecture and deploying a large system"

Related articles:

MyBatis Getting Started Basics (4)----Input Mapping and Output Mapping

"url mapping rules" and "server response order" in django

Related videos:

CSS3 Getting Started Tutorial

The above is the detailed content of How to map an http request to a method? Getting Started with SpringBoot: URL Mapping. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn