Ignore context-path for specific endpoint in Spring boot - java

I have setup the property server.servlet.context-path=/xyz in the application.properties file so all my endpoint have a prefix of /xyz. Now, I want to create an endpoint that should not have /xyz as the prefix(as an exceptional scenario). How can I achieve that?

Technically it is not possible because spring boot has only one DispatcherServlet which is a front controller, if you want two different paths then you can use #RequestMapping annotation on two different controllers`
Still of you want two different context-paths then you should have two DispatcherServlet's

Related

Disallow multiple slashes in a URL in Spring Boot

Is there any configuration in Spring Boot to make multiple slashes in a url unreachable?
Or how to make controller methods handle only correct path values?
Example (such requests should not be handled):
/api/v1/hello//world
/api/v1/hello/../hello/world

How to point a single spring actuator endpoint to the root ("/")

I have a requirement to expose a health endpoint on the root path on the specific port.
However, root path is reserved for the actuator endpoints overview and I could not find a way to overwrite that overview functionality with the specific endpoint functionality.
This DOES NOT work:
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=""
As a workaround, I created a rest controller which redirects "/" to "/actuator/health". But it looks ugly. Does anybody know a better solution?
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=/
setting the base-path to / disables the discovery endpoint as per doc,
When the management context path is set to /, the discovery page is disabled to prevent the possibility of a clash with other mappings.
This allows you to set the path mapping for any single actuator endpoint to /. You might use it for health, you might as well use it for any other one.
Note this is only possible when base-path is /. For any other base-path, trying to do path-mapping to / for any of the endpoints will give you
IllegalStateException: Ambiguous mapping. Cannot map 'Actuator root web endpoint' method
instead.
After conversations with multiple engineers, it seems like my solution with redirect is the only possible one.

Spring Boot - actuator- mappings : only own handler

is there a way in Spring Boot to display only my custom
Request Mappings with actuator? By default it shows everything.
As described in the reference documentation, the default behaviour of actuator endpoints, when the actuator module is on the classpath, is 'opt-out' - that is, most endpoints are enabled by default and must be disabled if required.
For the opposite effect (i.e. actuator endpoints must be specifically enabled), add the following setting to your application.properties:
management.endpoints.enabled-by-default=false
or alternatively, if using YAML:
management:
endpoints:
enabled-by-default: false
There is no way to filter which mappings get added. By default, anything with #RequestMapping is included.
You could always disable the provided mapping endpoint and write your own custom endpoint that includes only the controllers you care about.

Spring Boot - webservice client define multiple endpoints/profiles

How do I define multiple endpoints/configurations for my webservice client?
At the moment I've defined the webservice endpoint within application.properties
uri=https://foo.bar/endpoint
username=foo
password=bar
But I need to add several endpoints with different properties (username, password as an example), similar to spring-boot profiles but I have to change them request scoped.
Is there a mechanism to autowire my service request-scoped and use different profiles?
Is there another mechanism?

How to output all endpoints exposed by Spring

I'd like to have a way how to expose all endpoints that exposed by my Spring application. Is there a simple way to check, for each #profile which are exposed?
Example:
GET /api/resource
GET /api/resource/list
POST /api/resource
PUT /api/resource
In the past, I have used a web application made in Laravel, and they had a simple cli method for checking the exposed methods.
I assume based on how the questions is worded that you are not using Spring Boot, if you were, the actuator mappings endpoint does this for you, but your answer lies in how the mappings endpoint is build in actuator. There is a RequestMappingHandlerMapping object you leverage.
In this scenario you can use two approaches:
Spring Boot Actuator feature. Your endpoints of application will be available at http://host/actuator/mappings
Swagger library can also be used to list all endpoints of a REST API
The best solution is to use Spring boot actuator and hit the endpoint /actuator/mappings to get all the endpoints.
But if you can't use actuator or can't add it as dependency you can retrieve all the endpoints programmatically the mapping handlers, Spring get shipped with three implementations of this interface (HandlerMapping):
RequestMappingHandlerMapping: which is responsible for endpoints that annotated with #RequestMapping and its variants #GetMapping, #PostMapping .. etc
BeanNameUrlHandlerMapping: as the name suggest it will resolve the endpoint(URL) directly to a bean in the application context. for example if you hit the endpoint /resource it will look for a bean with the name /resource.
RouterFunctionMapping: it will scan the application context for RouterFunction beans and dispatch the request to that function.
Anyways, to answer your question you can autowire the bean RequestMappingHandlerMapping and print out all the handler methods. Something similar to this:
#Autowired
RequestMappingHandlerMapping requestMappingHandlerMapping;
#PostConstruct
public void printEnpoints() {
requestMappingHandlerMapping.getHandlerMethods().forEach((k,v) -> System.out.println(k + " : "+ v));
}

Categories