Springboot-url routes: @Controller and @requestmapping

Source: Internet
Author: User

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

Related Article

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.