Search code examples
javajsonspringspring-mvcspring-restcontroller

Should I use Spring restful web service or controllers to generate json?


I have built a web app using Spring 4. The app allows search users, create elements and so on.

The requirements changed. Now I have to deliver search results as json instead of html (but keep html just in case).

The question is, should I use controllers and redirect to a jsp with json/html according a parameter? or use Spring restful web service?(I've never used this and need to learn how to) if I use Spring restful do I have to do the same job I've already did with the web version to generate json?


Solution

  • It depends on how did you design your previous MVC application.

    My suggestion is follow:

    But better to use Spring REST Controller. Mechanism is more or less same like MVC. I assume you have service layer for CRUD data of your application.

    Better to use those Service and Repository and just create @RestController for your application. @ResponseBody should be your models which are you returning from controller. And @RequestBody will hold the data for generaly POST, PATCH, DELETE requests.

    Good luck.