How to keep following Endpoints as a property files in Spring Boot - spring

I have a requirement. for accomplish that i want to keep this end points as a property files in application property files.
Create Endpoint,
http://35.240.228.219/services/create_common.php
{"activeStatus":200,"outputObject":{},"settingsObject":{},"connectToken":"","inputObject":{"filterCriteria":"news","sourceLink":"dtv"},"sessionId":815100,"userId":171}
Delete Endpoint,
http://35.240.228.219/services/delete_common.php
{"activeStatus":1,"outputObject":[],"settingsObject":[],"connectToken":"","inputObject":{"filterCriteria":"news","sourceLink":"dtv"},"sessionId":815100,"userId":171}
Can anyone suggest me how to keep these kind of web endpoints.

In application.properties you can have below properties:
endpoint.create=http://35.240.228.219/services/create_common.php
endpoint.delete=http://35.240.228.219/services/delete_common.php
and from anywhere in your controller/service/class you can call them like this:
#Value("${endpoint.create}")
private String createEndPoint;
//use it anywhere..

Related

Spring Boot - How to add custom controller logic before user accesses static files

I'm working on a Spring Boot project, where some static contents are served from the src/main/resources/static directory.
My goal is that whenever a user tries to access static contents that end with a certain suffix (e.g. ".xlsx"), the request is intercepted and I check to see if the user has the right permission using Spring AOP, and reject the request if necessary. I've got the AOP part working in other scenarios, but not in this scenario yet.
Currently I've tried something like the following, but the method isn't being invoked upon accessing a file of ".xlsx" suffix:
#RequestMapping("/*.xlsx")
public void checkPermission() {
}
Can this be done without using Spring Security? Thanks in advance.
Have you tried Filter interface? much more available.
LINK: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/filter/OncePerRequestFilter.html
Using this you can easily parse the request before even it reaches the controller and add you business logic/validation to it.

Spring Boot: Retrieve config via rest call upon application startup

I d like to make a REST call once on application startup to retrieve some configuration parameters.
For example, we need to retrieve an entity called FleetConfiguration from another server. I d like to do a GET once and save the keep the data in memory for the rest of the runtime.
What s the best way of doing this in Spring? using Bean, Config annotations ..?
I found this for example : https://stackoverflow.com/a/44923402/494659
I might as well use POJOs handle the lifecycle of it myself but I am sure there s a way to do it in Spring without re-inventing the wheel.
Thanks in advance.
The following method will run once the application starts, call the remote server and return a FleetConfiguration object which will be available throughout your app. The FleetConfiguration object will be a singleton and won't change.
#Bean
#EventListener(ApplicationReadyEvent.class)
public FleetConfiguration getFleetConfiguration(){
RestTemplate rest = new RestTemplate();
String url = "http://remoteserver/fleetConfiguration";
return rest.getForObject(url, FleetConfiguration.class);
}
The method should be declared in a #Configuration class or #Service class.
Ideally the call should test for the response code from the remote server and act accordingly.
Better approach is to use Spring Cloud Config to externalize every application's configuration here and it can be updated at runtime for any config change so no downtime either around same.

SpringBoot custom spring.config.location

I have a simple SpringBoot application with the following structure:
I'm using a standard application.yml file where I'm storing all the necessary props and use #ConfigurationProperties annotation to inject them where necessary.
Now for one bean I have quite a lot of props and I don't want to overwhelm my common application.yml file with all that props. So I want a separate one (which I placed under service dir in classpath).
According to Spring docs I can use something like:
java -jar myproject.jar --spring.config.location=classpath:/service/application.yml
But that's not working, I got NullPointer which means property was not injected.
What Am I doing wrong? How can I use another *.yml file together with application.yml?
P.S. I know I could place it under the config folder in classpath, but what if I need two custom files?
If you have 2 configs in different places, spring.config.location will accept a comma separated list of those locations
--spring.config.location=classpath:/resources/,classpath:/service/
You could also just call the other file like "config.yml" and then use a different name
--spring.config.name=application,config

How to define explicit mapping for swagger-ui.html in spring boot application

I have an issue integrating swagger-ui (swagger2) into my spring-boot application. It seems as though the default location for swagger-ui being on the root is conflicting with an endpoint.
For example, my base path is foobar (full url: locahost:8080/foobar/). One of my endpoints has a path variable that can be any number (making the endpoint url: localhost:8080/foobar/{number} or localhost:8080/foobar/123 as an example). This seems to conflict with swagger-ui.html, as when I then try to hit locahost:8080/foobar/swagger-ui.html, spring complains, thinking 'swagger-ui.html' is the path variable for that endpoint.
I was curious if there was a way to define an explicit mapping for swagger-ui, so that spring won't think that I'm just inputting a path variable to the other endpoint? I ideally don't want to modify the foobar/{number} path at all, and would like to either make an explicit mapping for swagger-ui, or change the path for swagger-ui itself. Any help is appreciated. Thanks!

How to put together two application.yml configs of two projects?

I have two projects. One is just a client¹ that does http request to an external service. The other is a Rest API.
The client¹ reads an url configured in its own application.yml, but when I add a dependency (through pom.xml) to this client in the Rest API project, it no longer reads that property (it is null).
I want it to read from its own application.yml, so that I have a default value (that can be overridden later if necessary). How can I do that?
¹ The client is defined like this:
#Component
#ConfigurationProperties("app.client.notification")
public class NotificationClient {
private String url; // comes from application.yml.
// Works fine in the client project,
// and DOESN'T in the Rest project
}
You need to explicitly define the location of that .yml file somewhere in the code a'la #PropertySource("classpath:/application.yml"). I don't know about naming conflicts though, you need to experiment with it yourself.

Resources