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"; } } 
3
  • RestController is also a controller mixed with ResponseBody annotation. Please share what kind of error are you getting if using Controller. Commented Nov 10, 2018 at 17:54
  • Thats not javaee per se but Spring. Commented Nov 10, 2018 at 18:00
  • This might help: dzone.com/articles/… Commented Nov 10, 2018 at 18:35

2 Answers 2

2

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"; } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, problem solved. I was thinking right then, but I didn't knew about ResponseBody.
0
This is how I get something: Part in @RestController: @GetMapping("getSomething") public ResponseEntity<?> getSomething(){ return ResponseEntity.status(HttpStatus.OK).body(service.getSomeTHing()); } Part in @Service: public List<SomethingDTO> getSomething(){ List<Something> somethings = sr.findAll(); List<SomethingDTO> somethingsDTO = new LinkedList<>(); for (Something s: somethings) { SomethingsDTO dto = new SomethingsDTO(); BeanUtils.copyProperties(p, dto); somethingsDTO.add(dto); } return somethingsDTO; } And this is how I save something: RestController: @PostMapping("saveSomethings") public ResponseEntity<?> saveSomething(SomethingDTO something){ if (something.getSomething() == "") { throw new EmptyFieldsException("No fields can be empty."); } if (glumac.getSomething().length() != 7) throw new InvalidLengthException("Length must be 7", somethings.getSomething().length()); Integer idNewSomething = service.saveSomething(something); if (idNewSomething == -1) throw new SavingErrorException("Error while saving into database."); return ResponseEntity.ok("Success, new id is: "+idNewSomething); } Service: public int saveSomething(SomethingDTO something) { Something noviSomething = new Something(); noviSomething.setSomething(something.getSomething()); try { Something s = sr.save(newSomething); return s.getIdSomething(); } catch (Exception e) { e.printStackTrace(); return -1;`enter code here` } } 

}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.