The difference between @Controller and @restcontroller

Source: Internet
Author: User
Restcontroller and Controller

Assume a user object that has many properties (Name,sex,age,birth,address,tel)
In my understanding, the distinction between the two is: @restcontroller for the combination of @controller and @responsebody
In the @controller annotation, the string is returned, or the template name that matches the string , that is, the directly rendered view, which is used with the HTML page,
In this case, the front and back of the matching requirements are relatively high , the Java backend code to be rendered in conjunction with HTML, the use of the Model object (or Modelandview) data will populate the user view of the relevant properties, and then show to the browser, This process can also be called rendering;
The Java sample code is as follows:

@Controller
@RequestMapping (method = requestmethod.get, value = "/") Public
    String GetUser (model model) throws IOException {

        model.addattribute ("name", Bob);
        Model.addattribute ("Sex", boy);
        Return "user",//user is the template name
    }

corresponding to the HTML code in the View user.jsp:
object , that is, return a user Object , when there is no page , you can also see the return of a user  Object , and the function of the frontend is to parse the rendered page with the returned JSON, and the Java backend code is more free.

Java-side code:

@RestController
@RequestMapping (method = requestmethod.get, value = "/") Public
    User GetUser () throws IOException {
        user bob=new User ();
        Bob.setname ("Bob");
        Bob.setsex ("Boy");
        return Bob;
    }

The URL gets the JSON string {"name": "Bob", "Sex": "Boy"}, the front page only needs to handle this string to

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.