Controller or RestController









up vote
1
down vote

favorite
1












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";











share|improve this question























  • 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














up vote
1
down vote

favorite
1












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";











share|improve this question























  • 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












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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";











share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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";







share|improve this answer




















  • Thanks, problem solved. I was thinking right then, but I didn't knew about ResponseBody.
    – Minirock
    Nov 10 at 18:12










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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";







share|improve this answer




















  • Thanks, problem solved. I was thinking right then, but I didn't knew about ResponseBody.
    – Minirock
    Nov 10 at 18:12














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";







share|improve this answer




















  • Thanks, problem solved. I was thinking right then, but I didn't knew about ResponseBody.
    – Minirock
    Nov 10 at 18:12












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";







share|improve this answer












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";








share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus