Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Spring controller and Endpoint

Spring boot actuator provides some endpoints like health, metrics, info. It also allows us to write our own custom endpoints.

I have a requirement where I need to expose some Dropwizard metrics stats as an endpoint. Latest Spring-boot does support dropwizard metrics but it does not fit into my requirement, so I am planning to have my own web endpoint /stats

But now I am not able to decide whether it should be a normal Controller or a custom actuator Endpoint. What's the difference between these two terms?

PS: question does seem opinion base, but the answer should be simple enough.

like image 271
sidgate Avatar asked Mar 15 '16 10:03

sidgate


People also ask

What is an endpoint in spring?

Actuator endpoints let you monitor and interact with your application. Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health endpoint provides basic application health information. Each individual endpoint can be enabled or disabled.

What is the difference between spring controller and rest controller?

@Controller is used to mark classes as Spring MVC Controller. @RestController annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialized version of @Component annotation.

What is a spring controller?

Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It's used to mark a class as a web request handler. It's mostly used with Spring MVC applications.


1 Answers

Endpoints are a more specific or peculiar version of a Controller.

Rather than rely on a view (such as JSP) to render model data in HTML, an endpoint simply returns the data to be written directly to the body of the response(Similar to doing @ResponseBody in Controller).

Actuator Endpoint is a better option because of the following reasons :

  1. Endpoints are meant to perform the highly specific task of printing your Object(Json) on HTTP which is exactly what you want to do here.
  2. To separate monitor-n-manage code from your application-specific code.
  3. To keep things cleaner and cohesive
like image 176
Manali Bhosale Avatar answered Oct 29 '22 08:10

Manali Bhosale