Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to resolve REST end points of a dependency JAR file in spring Boot

I have a 2 Spring boot jars which work fine as 2 independent applications, however, I have been asked to merge 2 jars into a single application

The easiest thing I thought would be to add app-2 as a maven dependency into app-1 but the problem is that when the app-1 starts it only recognises the app-1 REST endpoints but ignores REST endpoint of app-2 altogether.

I was hoping that when the app-1 starts it will automatically pick the endpoints declared in app-2

@RestController
Class2{

@GetMapping(/hello-from-app2)
public String myapp2(){
  return "HELLO FROM APP2"
}

This code gets ignored and at server start up I can only see the endpoints exposed by app-1 are visible.

like image 323
Abhishek Galoda Avatar asked Oct 11 '18 16:10

Abhishek Galoda


People also ask

How do you find the endpoint of a spring boot?

In a Spring Boot application, we expose a REST API endpoint by using the @RequestMapping annotation in the controller class. For getting these endpoints, there are three options: an event listener, Spring Boot Actuator, or the Swagger library.

What is REST endpoint in spring boot?

Now, what is a REST Endpoint? A REST endpoint makes it possible for a web application to respond to HTTP REST requests. Therefore, if a user goes to the browser and enters a url, we want to tell spring to run a specific method and send the return value to the user.

Which of the following can be used for dependency managment in spring boot?

UTF-8 source encoding. It inherits a Dependency Section from the spring-boot-dependency-pom. It manages the version of common dependencies.


2 Answers

If you are including App2.jar as a dependency into App1.jar, the best approach would be to import the Configuration of App2. If you start adding scans and stuff you would be tightly coupling you App1 to your App2. App1 would have to know implementation details of App2 that doesn't need to.

If you just import the configuration of App2, the configuration details would remain encapsulated.

I assume you have a Java Config class (or an XML Config file) for App1 and another one for App2. I also assume that the config of App2 contains all the necessary annotations for component scanning and the correct base-packages.

If that's the case, you can add an import like this and it should work right away:

@Configuration
@Import(SpringConfigurationApp2.class)
public class SpringConfigurationApp1 {

//... some beans....

}
like image 56
Diego Avatar answered Oct 29 '22 12:10

Diego


I saw answer provided by @Diego but with that user need to make changes in client application. (ex. @Import(SpringConfigurationApp2.class) here).

I have another approach where client (App-1) does not need to make any change in application. It will just work seamlessly. This approach is by use of spring's auto configuration and same feature is used by spring-boot dependency.

Here is my answer to achieve using autoconfiguration.

For App-2,

1) create spring.factories file under resources/META-INF

2) Add org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ path-to-app-2-application/SpringConfigurationApp2

For App-1, Just include App-2 as maven dependency and you are Done.

Here is a link to get more information about https://dzone.com/articles/what-is-spring-boot-auto-configuration.

like image 22
Shaunak Patel Avatar answered Oct 29 '22 13:10

Shaunak Patel