46523

Controller or RestController

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

Answer1:

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

Recommend

  • Why do functions return `undefined` instead of `null` by default?
  • Controling HTML5 video with jQuery
  • How to recreate a virtual env in python
  • mat tab inside tab selected index not working
  • Respond to Website Message Box by Automating InternetExplorer with VBA
  • angular 2 websql typings
  • PhoneGap FileReader/readAsDataURL Not Triggering Callbacks
  • Android How to call a method multiple times with a delay between them
  • Encoding/decoding PDP-11 assembly language to binary and hex?
  • PostgreSQL in Docker - pg_hba.conf to allow access from host to container
  • search bar getting disappeared in ios UIsearchcontroller
  • Wicket countdown timer will not self-update
  • Reshape dataframe to dataframe with unlimited rows and filling zeroes where no values
  • Weekday as String to number
  • “RepeatForUnit” item missing in Calendar entry?
  • JSF validateLength question
  • Trying to get lotusscript json reader
  • what is “Other” category in CosmosDB monitoring graph?
  • Pyinstaller GLIBC_2.15 not found
  • CABasicAnimation creates empty default value copy of CALayer
  • Spongycastle is missing many algorithms when loaded on android
  • When i select a Textfield the keyboard moves over it
  • Passing HTTP Post from AWS API GW to Lambda
  • Jekyll - How do I create pages in the root directory?
  • How to control xtics in gnuplot
  • Create an Office365 mailbox from within C# Web API method
  • trigger ontouch event programmatically
  • Creating 2d platforms using JavaScript
  • How to merge objects within array based on attribute
  • How to write seo friendly url's using htaccess?
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • How to split wav file into two or more parts using c#
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • WPF custom control and direct content support
  • media foundation H264 decoder not working properly
  • How to check if object is null in Java?