Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One entity class with two controllers (@RestController and @Controller)

I am trying to build a web application that handles both @RestController and @Controller. As far as I understand that @RestController is for API testing using postman to handle crud processes on the other hand @Controller is to handle crud in model view. Is it possible to create both of these controllers for one entity class in Spring boot application and make them both call the same method from the service class which handles the logic of the crud implementation? Please provide an example to your explanation with an entity class and both of the controllers to clear the idea for me.

like image 203
RizoToma Totta Avatar asked Jun 23 '26 03:06

RizoToma Totta


2 Answers

The approach I would follow is to use @Controller on the class level and then use @RequestBody + Content-type as "application/json" on method level for API testing from Postman.

And for model view will not use @RequestBody and specify Content-type as "text/html" and return page from the method.

Sample code snippets :

For API testing :

@RequestMapping(value="/orders", method=RequestMethod.GET, produces="application/json")
@ResponseBody
public List<Order> getOrders {
    return orderManager.getAllOrders();
}

For Model :

@RequestMapping(value="/accounts", method=RequestMethod.GET,produces="text/html")
public String accountSummary() {
    // Put data into model and return view name
    return "summary";
}
like image 107
Alien Avatar answered Jun 25 '26 18:06

Alien


  1. RestController is a meta-annotation of Controller and ResponseBody annotation. The annotation was bought in from Spring 4.0 version which helps you design controllers whose response is not a ModelandView but instead, it return value is raw data in the form of JSON/XML (There are other types of MediaType which can be defined using produces and consumes parameter of the @RequestMapping)
  2. When you define a controller, the dispatcher servlet talk to the ViewResolver to resolve the returned String to a view/page. In case @RestController, dispatcher servlet uses HttpMessageConverters to send back the raw response to the client in a request format - json/xml.
  3. So REST API is an endpoint which helps you expose your resources over the network for another SERVICE to consume; Spring helps you write REST API using @RestController classes. In case you working on a web based application with jsp/thymeleaf as a rendering engine for human interaction with the application, you will work with @Controller.

So, what you wrote :

@RestContrller is for API testing using postman to handle crud processes on the other hand @Controller is to handle crud in model view

is not entirely correct. You build REST API using @RestController. You test the endpoints using Postman, but @RestController is not used for API testing using postman. You use it to build the endpoint and test it with postman(you can use other clients and frameworks to test your endpoint too).

Answering your question:

Is it possible to create both of these controllers for one entity class in Spring boot application and make them both call the same method from the service class which handles the logic of the crud implementation?

Yes, it is possible to have @REstController and @Controller working with same service class for the same entity (in fact if business logic is same for your view and response in json- you will just have one service bean). It is quite possible that your application has a front-end built using jsp for human users and also expose endpoints to interact with other services; this is generally relevant in big companies where multiple services rely on each other for functionality and data. But you need to make sure your endpoints are not colliding or the same. Normally if we have a controller mapping as /books -> displays a list of books on the screen, we will have a similar rest controller mapping - /api/v1/books -> to return a list of books.

like image 22
Priyak Dey Avatar answered Jun 25 '26 19:06

Priyak Dey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!