Spring - Migrating from MVC to Rest

Asked

Viewed 271 times

-4

I did a CRUD with Spring Boot, MVC, Security and other Spring modules. For the front-end I used Thymeleaf. Apparently everything is working, but in the problem description asks "The application should contain some Rest Apis for external consumption: registration and consultation of users and resources".

What should I change on my app? Switch from @Controller to @Restcontroller? controller method returns can continue to return Modelview and String or have to return Responseentity? Can the front end continue with Thymeleaf? What exactly does the external consumption of the Rest API have to do? Only answer the JSON request to Postman?

  • Hello good morning leo, yes my dear @Restcontroller, Modelandview not because now you will not have a View, the Api will be consumed by any client so JSP, JSF, PHP, Postman, and or even a Web application Spring +Thymeleaf, HTML + Jq, who consume will have to indicate the end-point ex http://localhost:8080/api/users the return may be a JSON but it may be an XML, Text ..... This is negotiable, go ahead you’re close.

1 answer

0


You’re complicating the requirement a little bit. By asking:

"The application should contain some Rest Apis for external consumption: registration and consultation of users and resources".

It doesn’t mean you need to use the same Screen Controller for this. Existing Controllers, which return ModelAndView, continue as they are today.

What you can do is create a new Controller, in this case, a RestController (or more than one, if you prefer).

It will be solely responsible to meet these external consumptions, thus being able to define the output format (Json), security, API versioning, different access address, etc, without interfering with existing Controllers.

Of course, the code of Service and Repository will be shared internally between Controllers and Restcontrollers to avoid code duplication and so on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.