Controller or RestController
up vote
1
down vote
favorite
i'm new to jEE, and this is my first jEE code using spring. The code bellow is working fine. He just print the string index when i go to my localhost; and otherwise he print handling error.
My question is: Why this code isn't working anymore if I use @Controller instead of @RestController
I can't find any simple explanation in the docs from spring and I was hoping someone could explain this.
I have the feelings that a controller alone can't work without something like thymeleaf (I know if I were using thymeleaf the string index would be replaced by the index page from the ressources folder) where a RestController might be returning data as xml or json or something else.
Thanks
@RestController
public class HelloController implements ErrorController
@RequestMapping("/")
public String index()
return "index";
@RequestMapping("/error")
public String error()
return "gestion erreur";
@Override
public String getErrorPath()
return "/error";
spring spring-mvc spring-boot
add a comment |
up vote
1
down vote
favorite
i'm new to jEE, and this is my first jEE code using spring. The code bellow is working fine. He just print the string index when i go to my localhost; and otherwise he print handling error.
My question is: Why this code isn't working anymore if I use @Controller instead of @RestController
I can't find any simple explanation in the docs from spring and I was hoping someone could explain this.
I have the feelings that a controller alone can't work without something like thymeleaf (I know if I were using thymeleaf the string index would be replaced by the index page from the ressources folder) where a RestController might be returning data as xml or json or something else.
Thanks
@RestController
public class HelloController implements ErrorController
@RequestMapping("/")
public String index()
return "index";
@RequestMapping("/error")
public String error()
return "gestion erreur";
@Override
public String getErrorPath()
return "/error";
spring spring-mvc spring-boot
RestController is also a controller mixed with ResponseBody annotation. Please share what kind of error are you getting if using Controller.
– Avinash Sagar
Nov 10 at 17:54
Thats not javaee per se but Spring.
– Antoniossss
Nov 10 at 18:00
This might help: dzone.com/articles/…
– Rishabh Agarwal
Nov 10 at 18:35
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
i'm new to jEE, and this is my first jEE code using spring. The code bellow is working fine. He just print the string index when i go to my localhost; and otherwise he print handling error.
My question is: Why this code isn't working anymore if I use @Controller instead of @RestController
I can't find any simple explanation in the docs from spring and I was hoping someone could explain this.
I have the feelings that a controller alone can't work without something like thymeleaf (I know if I were using thymeleaf the string index would be replaced by the index page from the ressources folder) where a RestController might be returning data as xml or json or something else.
Thanks
@RestController
public class HelloController implements ErrorController
@RequestMapping("/")
public String index()
return "index";
@RequestMapping("/error")
public String error()
return "gestion erreur";
@Override
public String getErrorPath()
return "/error";
spring spring-mvc spring-boot
i'm new to jEE, and this is my first jEE code using spring. The code bellow is working fine. He just print the string index when i go to my localhost; and otherwise he print handling error.
My question is: Why this code isn't working anymore if I use @Controller instead of @RestController
I can't find any simple explanation in the docs from spring and I was hoping someone could explain this.
I have the feelings that a controller alone can't work without something like thymeleaf (I know if I were using thymeleaf the string index would be replaced by the index page from the ressources folder) where a RestController might be returning data as xml or json or something else.
Thanks
@RestController
public class HelloController implements ErrorController
@RequestMapping("/")
public String index()
return "index";
@RequestMapping("/error")
public String error()
return "gestion erreur";
@Override
public String getErrorPath()
return "/error";
spring spring-mvc spring-boot
spring spring-mvc spring-boot
edited Nov 11 at 8:30
Rishabh Agarwal
745317
745317
asked Nov 10 at 17:48
Minirock
182
182
RestController is also a controller mixed with ResponseBody annotation. Please share what kind of error are you getting if using Controller.
– Avinash Sagar
Nov 10 at 17:54
Thats not javaee per se but Spring.
– Antoniossss
Nov 10 at 18:00
This might help: dzone.com/articles/…
– Rishabh Agarwal
Nov 10 at 18:35
add a comment |
RestController is also a controller mixed with ResponseBody annotation. Please share what kind of error are you getting if using Controller.
– Avinash Sagar
Nov 10 at 17:54
Thats not javaee per se but Spring.
– Antoniossss
Nov 10 at 18:00
This might help: dzone.com/articles/…
– Rishabh Agarwal
Nov 10 at 18:35
RestController is also a controller mixed with ResponseBody annotation. Please share what kind of error are you getting if using Controller.
– Avinash Sagar
Nov 10 at 17:54
RestController is also a controller mixed with ResponseBody annotation. Please share what kind of error are you getting if using Controller.
– Avinash Sagar
Nov 10 at 17:54
Thats not javaee per se but Spring.
– Antoniossss
Nov 10 at 18:00
Thats not javaee per se but Spring.
– Antoniossss
Nov 10 at 18:00
This might help: dzone.com/articles/…
– Rishabh Agarwal
Nov 10 at 18:35
This might help: dzone.com/articles/…
– Rishabh Agarwal
Nov 10 at 18:35
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
The job of @Controller is to create a Map of model object and find a view but @RestController simply return the object and object data is directly written into HTTP response as JSON or XML.
The @Controller is a common annotation which is used to mark a class as Spring MVC Controller while @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody.
If you want the same functionality of @RestController without using it you can use @Controller and @ResponseBody.
@Controller
public class HelloController
@RequestMapping("/")
@ResponseBody
public String index()
return "index";
Thanks, problem solved. I was thinking right then, but I didn't knew about ResponseBody.
– Minirock
Nov 10 at 18:12
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The job of @Controller is to create a Map of model object and find a view but @RestController simply return the object and object data is directly written into HTTP response as JSON or XML.
The @Controller is a common annotation which is used to mark a class as Spring MVC Controller while @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody.
If you want the same functionality of @RestController without using it you can use @Controller and @ResponseBody.
@Controller
public class HelloController
@RequestMapping("/")
@ResponseBody
public String index()
return "index";
Thanks, problem solved. I was thinking right then, but I didn't knew about ResponseBody.
– Minirock
Nov 10 at 18:12
add a comment |
up vote
0
down vote
accepted
The job of @Controller is to create a Map of model object and find a view but @RestController simply return the object and object data is directly written into HTTP response as JSON or XML.
The @Controller is a common annotation which is used to mark a class as Spring MVC Controller while @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody.
If you want the same functionality of @RestController without using it you can use @Controller and @ResponseBody.
@Controller
public class HelloController
@RequestMapping("/")
@ResponseBody
public String index()
return "index";
Thanks, problem solved. I was thinking right then, but I didn't knew about ResponseBody.
– Minirock
Nov 10 at 18:12
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The job of @Controller is to create a Map of model object and find a view but @RestController simply return the object and object data is directly written into HTTP response as JSON or XML.
The @Controller is a common annotation which is used to mark a class as Spring MVC Controller while @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody.
If you want the same functionality of @RestController without using it you can use @Controller and @ResponseBody.
@Controller
public class HelloController
@RequestMapping("/")
@ResponseBody
public String index()
return "index";
The job of @Controller is to create a Map of model object and find a view but @RestController simply return the object and object data is directly written into HTTP response as JSON or XML.
The @Controller is a common annotation which is used to mark a class as Spring MVC Controller while @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody.
If you want the same functionality of @RestController without using it you can use @Controller and @ResponseBody.
@Controller
public class HelloController
@RequestMapping("/")
@ResponseBody
public String index()
return "index";
answered Nov 10 at 18:08
Srinivasu Kotla
343
343
Thanks, problem solved. I was thinking right then, but I didn't knew about ResponseBody.
– Minirock
Nov 10 at 18:12
add a comment |
Thanks, problem solved. I was thinking right then, but I didn't knew about ResponseBody.
– Minirock
Nov 10 at 18:12
Thanks, problem solved. I was thinking right then, but I didn't knew about ResponseBody.
– Minirock
Nov 10 at 18:12
Thanks, problem solved. I was thinking right then, but I didn't knew about ResponseBody.
– Minirock
Nov 10 at 18:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241770%2fcontroller-or-restcontroller%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
RestController is also a controller mixed with ResponseBody annotation. Please share what kind of error are you getting if using Controller.
– Avinash Sagar
Nov 10 at 17:54
Thats not javaee per se but Spring.
– Antoniossss
Nov 10 at 18:00
This might help: dzone.com/articles/…
– Rishabh Agarwal
Nov 10 at 18:35