spring boot common controller prefix - spring-boot

I need to set a prefix for my controllers: /api. But this shouldn't impact the actuator endpoints eg /health. I know you can set the server.context-path but this will also impact the actuator endpoints. I know i can also put an annotation on each controller, but this could easily lead to problems when some other developer forgets it. So is there an easy way to have this prefix on each custom controller but not the spring boot actuator endpoints? Don't really car if it is with java config or some property that i haven't found yet.
Thanks

Related

How to prefix all controller paths but not management endpoints?

I have a Spring Boot application with a lot of controllers, and I want to run the service on a sub-path of an existing domain. So I've figured it would be nicer to have this configurable than to hardcode the path into all the request mappings.
I've considered all the approaches mentioned here https://www.baeldung.com/spring-boot-controllers-add-prefix, but they either also change the prefix of the actuator endpoints or are not globally enforced, so I'd prefer a different approach.
I want the actuator endpoints on /_system/ (changed by management.endpoints.web.base-path=/_system) and have only the /prefix as a load balancer target, so the management endpoints won't ever be reachable from the internet (in case I miss-configure the application and start leaking some sensitive info from the management endpoints).
I've also tried configuring management.server.base-path=/, but that seems to also be affected by the server.servlet.context-path and spring.mvc.servlet.path as the actuator seems to be registered "under" the main servlet dispatcher.
I was looking into registering a separate servlet dispatcher just for the actuator endpoints, but I've given up on researching it after some time because I feel like I'd have to override half of Spring Boot.
Any other ideas? Thanks

Custom Spring Actuator Endpoint which has subsystem and can be added dynamically

I'm looking for a way to implement custom endpoints for a reactive application using Spring Boot 2.2.
The endpoints have some subsystems and perform specific resource operations on the subsystems. The URL paths look like:
/actuator/system1/subsystem_a
/actuator/system1/subsystem_b
/actuator/system2/subsystem_c
Furthermore, system1 and system2 are not both always deployed, so I'd like to add dynamically the endpoints of the deployed system only.
I know I can use ReactiveHealthContributorRegistry to add custom health check endpoints dynamically. Is there a similar way for a fully custom endpoint?
Thanks in advance.
It seems there is no way to construct such complex endpoints like what I asked in Spring Boot Actuator.
I finally decided to use RouterFunction and HandlerFunction referring to the following websites.
https://www.baeldung.com/spring-5-functional-web
https://spring.io/blog/2016/09/22/new-in-spring-5-functional-web-framework

Listing all deployed rest endpoints (spring-boot, tomcat)

I know there is a similar kind of question exist but if works only for glassfish server.
Listing all deployed rest endpoints (spring-boot, jersey)
Is it possible to list all my configured rest-endpoints with spring boot? The actuator lists all existing paths on startup, I want something similar for my custom services, so I can check on startup if all paths are configured correctly and use this info for client calls.
How do I do this? I use #Path/#GET annotations on my service beans and register them via ResourceConfig#registerClasses.
Is there a way to query the Config for all Paths?
Update2: I want to have something like
GET /rest/mycontroller/info
POST /res/mycontroller/update
...
In my opinion, you are already using the right tool (actuator) to answer to your request.
Actuator gives you all the rest method running and you can configure it on your own, by disabling/enabling specific endpoints
If you have a look on the documentationprobably it can help you.
In any case, the default configuration of actuator display the endpoints (built-in in Intellij for your development).

How to enable cross-origin in spring -boot for micro-services

I have nearly 5 spring boot applications which are connected to discovery service to communicate with each other. and also I want to expose some controllers to the public, so how can I enable cross-origin? should I do it in all controller level or from discovery? Any other suggestion Please!
Use the #CrossOrigin annotation either on your controller classes or on each of your controller methods that you want to permit.
You can also create a WebMvcConfigurer Bean in one of your configuration classes where you can establish cross origin configuration at a global level.
Edit: I like #peekay suggestion as well.
I have used Kong Api Gateway in front of all services. To enable cross origin.its very easy to do it in single place rather than in every single controller

Spring Boot Actuator endpoints with set server.context-path

In spring boot app I set e.g. server.context-path=/mymodule. This is convenient because I don't need to repeat over and over again /mymodule prefix in #RequestMapping.
Further I want to have actuator endpoints grouped together on URLs with common prefix so I set management.context-path=/actuator.
Now actuator endpoint are mapped to /mymodule/actuator.
From security perspective I want to have actuator endpoints mapped to /actuator. Simple config on reverse proxy https://mydomain/api/mymodule -> http://oneofmyserver:port/mymodule protects that end users would not be able to access actuator.
Is it possible to map actuator endpoints to /actuator?
Probably better solution from security perspective is to export actuator on totally different port. To do it just add such properties:
management.port=9080
You can also just change context-path of actuator endpoints by using
management.context-path=/actuator
You can use the management.endpoints.web.base-path property to change the prefix for your management endpoint. Reference from spring-boot document.
The following example remaps /actuator/health to /healthcheck;
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck
In case of you aren't able to use different ports and you have, for example, eureka in you environment, you can use the following:
Configure additional metadata, that you send to eureka. Simply by adding there some parameter with value of your context-path. spring docs
Configure Prometheus for using Eureka.
After that you will get an access to your custom meta-data
Finally you can use these new parameters in a relabeling step.

Resources