How to prefix all controller paths but not management endpoints? - spring

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

Related

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).

spring boot common controller prefix

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

Zuul proxy that discovers routes dynamically

I have a simple Zuul app that has a single route in the application.yml to route to my microservice. It's working.
However, what I'm looking for is a more dynamic solution where I can wire up routes dynamically, either through code or perhaps by POSTing to some Zuul endpoints during a build (possibly by using springfox and a swagger definition from microservices). I could not find an API for Zuul.
I'm somewhat aware of Eureka and that seems like a solution to abstract away the routing by doing discovery. However, I'm curious if there's a solution without introducing Eureka. If there's a way to wire up these routes in Zuul during a build vs. having to edit the application.yml every time.
Thanks in advance.
If you go for Eureka this will actually work ootb. Zuul as packaged in spring cloud will automatically expose every service using its name. So if you register a service called users in Eureka, Zuul will automatically create a route /users forwarding to the instances by default. That will only allow simple url structures but should solve your problem.
Please see the official documentation for details:
By convention, a service with the ID "users", will receive requests from the proxy located at /users (with the prefix stripped). The proxy uses Ribbon to locate an instance to forward to via discovery, and all requests are executed in a hystrix command, …
I'm actually editing a blog post about this exact topic (Routing and Filtering using Spring Cloud Zuul Server) but the source code has been available and working for some time now. Feel free to use it as a reference:
https://bitbucket.org/asimio/zuulserver
https://bitbucket.org/asimio/discoveryserver (in case routes are configured with serviceIds)
https://bitbucket.org/asimio/demo-config-properties/src (Zuul-Server-refreshable.yml where routes are dynamically updated).
Look at the refreshable Spring profile settings. This Zuul setup works with both, hard-coding routes url or discovered using Eureka.
It also acting as a Spring Cloud Config client so that routes could be dynamically updated via Git, which is also covered in another blog post: Refreshable Configuration using Spring Cloud Config Server, Spring Cloud Bus, RabbitMQ and Git.

Sharing of a view among microservies

I'm splitting up a monolith web service into several microservices using spring boot. To reduce duplicated code I extracted shared parts in a maven module that is used in the different microservices.
The monolith application had a healthcheck page that showed various information about the state of the service and some debbuging infos. It is implemented with Spring MVC and jsp.
I'd like to use this view in each of the microservices. Whats the best way to do this without duplicating the view/controller?
I was thinking of adding a web module to the shared maven project that contains the controller, view, spring mvc settings,...
But I'm not sure if it is good to have two web modules in one microservice.
Have you considered using spring boot actuator to retrieve health (and more) application information?
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready
You could then have another microservice that retrieves that information from each of your services, or just simply check it on then hitting the different endpoints (/health, /env, etc.).
UPDATE:
If you have you custom health logic you can even implement your own actuator endpoint for it. Furthermore, you can create your own library to reuse it in all your microservices:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-customizing-endpoints-programmatically
46.4 Adding custom endpoints
If you add a #Bean of type Endpoint then it will automatically be exposed over JMX and HTTP (if there is an
server available). An HTTP endpoints can be customized further by
creating a bean of type MvcEndpoint. Your MvcEndpoint is not a
#Controller but it can use #RequestMapping (and #Managed*) to expose
resources.
[Tip]
If you are doing this as a library feature consider adding a
configuration class annotated with #ManagementContextConfiguration to
/META-INF/spring.factories under the key
org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration.
If you do that then the endpoint will move to a child context with all
the other MVC endpoints if your users ask for a separate management
port or address. A configuration declared this way can be a
WebConfigurerAdapter if it wants to add static resources (for
instance) to the management endpoints.

Resources