Spring Boot - webservice client define multiple endpoints/profiles - java

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?

Related

Spring Boot Actuator - Custom Endpoints

I am using Spring Boot Actuator module in my project which exposes REST endpoint URLs to monitor & manage application usages in production environment, without coding & configuration for any of them.
By default, only /health and /info endpoints are exposed.
I am customising the endpoints via application.properties file as per my use case.
application.properties.
#To expose all endpoints
management.endpoints.web.exposure.include=*
#To expose only selected endpoints
management.endpoints.jmx.exposure.include=health,info,env,beans
I want to understand, where exactly does Spring Boot create actual endpoints for /health and /info and how does it expose them over HTTP?
Thanks #Puce and #MarkBramnik for helping me out with the reference docs & code repository.
I wanted to understand how the endpoints were working and how they were exposed over HTTP, so that I could create custom endpoints to leverage in my application.
One of the great features of Spring Framework is that it’s very easy to extend, and I was able to achieve the same.
To create a custom actuator endpoints, Use #Endpoint annotation on a class. Then leverage #ReadOperation / #WriteOperation / #DeleteOperation annotations on the methods to expose them as actuator endpoint bean as needed.
Reference Doc : Implementing Custom Endpoints
Reference Example :
#Endpoint(id="custom_endpoint")
#Component
public class MyCustomEndpoint {
#ReadOperation
#Bean
public String greet() {
return "Hello from custom endpoint";
}
}
The endpoint id i.e custom_endpoint needs to be configured in the list of actuator endpoints to be enabled.
application.properties :
management.endpoints.web.exposure.include=health,info,custom_endpoint
After a restart, endpoint works like a charm!

Multi tenant configuration spring boot

I wonder if there is a good solution to manage different configurations for different tenants in runtime, the same instance should manage all the tenants.
Currently, the application gets all the properties using #Value but this inject the properties in the application runs, and the property is gonna be different depends on the tenant making the request.

how spring handles Multiple login request as default scope is singleton?

How, Spring controllers handle multiple request ? As default scope of bean is singleton and when the application will deploy in server multiple user will try to access the landing page. As the default scope is singleton then how spring MVC handles request ?
From your application server, each thread will be created for serving your each http requests. Controller instance will be only one per server as being singleton.All the requests will share the same controller instance.

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));
}

How to access spring mvc properties value in websocket onopen event?

I am using spring mvc + websocket. I have created application.properties to store configuration data of project. Now I want to use that properties values in websockets #onOpent() event. I am able to access those properties in simple rest controller #RequestMapping() but unfortunately I am not able to access those properties in in websockets #onOpent() event, I am getting null value for the same. How can I achieve this?
This happens because Websocket server class in not spring component.
you can mark websocket handler class as component using #Component annotation on class. Then you will be able to access properties.
Hope this helps you.
Seems like you are using Java websockets. you can use spring 4 websockets here is sample

Categories