3

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.

dhana1310
  • 479
  • 1
  • 5
  • 16

3 Answers3

3

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";
}
Alien
  • 15,141
  • 6
  • 37
  • 57
3
  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.

Priyak Dey
  • 1,227
  • 8
  • 20
0

@RestController is a @Controller + @ResponseBody. You don't need two controllers. Just use one @RestController and it will work for both your cases.

aboudirawas
  • 192
  • 3
  • 21
  • 2
    RestController does not return a view. That's I am asking how to handle a view and data testing from both or either one of them. I am not clear at this point. I have made some research but most of them said that REST can not return a view. I need to handle the view and the returning data from an entity class. – RizoToma Totta Mar 27 '20 at 12:34
  • are you using thymeleaf for your views? – aboudirawas Mar 27 '20 at 12:53