Springboot defining URL Handling methods: @Controller and @requestmapping
@Controller labeled class represents a controller that handles HTTP requests (that is, C in MVC), and all methods that are @RequestMapping labeled in that class are used to handle requests for the corresponding URLs.
In the SPRINGMVC framework, you can use @requsetmapping annotations to bind URLs to processing methods, for example:
@RestController Public class Helloworldrestcontroller { @RequestMapping ("/") public String HelloWorld () { return "Hello World"; } @RequestMapping ("/hello") @ResponseBody public String Hello () { return "FPC"; }}
The Helloworldrestcontroller class is labeled by @controller, where two of the methods are @requestmapping labeled and accessed in the browser after the application is run: localhost:8089, Requests are processed by the SPRINGMVC framework that is distributed to the Hellworld () method. The same input Localhost:8089/hello will be handed to the Hello () method for processing.
@ResponseBody callout Indicates that the handler function directly passes the return value of the function to the browser side display.
Operation Result:
Input localhost:8089:
Input Loalhost:8089/hello:
@RequestMapping Label class
@RequestMapping annotations can also be added to the class:
@RestController @requestmapping ("/index") Public class Helloworldrestcontroller { @RequestMapping ("/") public String HelloWorld () { return "Hello World"; } @RequestMapping ("/hello") @ResponseBody public String Hello () { return "FPC"; }}
The URL path of the Hello () method binding is/index/hello
Operation Result:
If direct access: Localhost:8089/hello:
If you visit: Localhost:8089/index/hello:
Tip: Each class can contain one or more methods of @requestmapping annotations, and often we will place URLs with similar business logic in the same controller.
Springboot-url routes: @Controller and @requestmapping