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