Spring Boot API bootstrap creator without Controllers
Required Java version: 11
Project consists of 3 modules:
- Core module with Spring dependencies
- Spring Boot autoconfigure module
- Spring Boot Starter module
The aim of this project is to provide a convenient way to configure API with Spring Boot
This project also allows developers to manage API through Spring Cloud Config when using Spring Cloud technologies
- Add Exposer Spring Boot Starter & Spring Boot Starter Web dependencies to your project
with Maven:
<dependencies>
<dependency>
<groupId>com.github.kuzznya</groupId>
<artifactId>exposer-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- ... -->
</dependencies>with Gradle:
implementation 'com.github.kuzznya:exposer-spring-boot-starter:1.1.2'
implementation 'org.springframework.boot:spring-boot-starter-web'- Add
@EnableExposerannotation on Spring Boot Application class
import com.github.kuzznya.exposer.EnableExposer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableExposer
public class ExposerSampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExposerSampleApplication.class, args);
}
}- Configure your API endpoints - see next section
There are two types of configuration
Subroute config:
| Property | Required | Description |
|---|---|---|
path |
true | Relative path of this subroute |
routes |
false | Nested routes of this subroute |
endpoints |
false | Endpoints of this subroute |
bean |
false | Bean that you want to expose |
Endpoint config:
| Property | Required | Description |
|---|---|---|
http-method |
true | HTTP method of endpoint (GET, POST, etc.) |
bean-method |
true | Name of respective bean method |
bean |
false | Bean that you want to expose |
params |
false | Bean method params mapping |
Endpoint params config:
To map request param to method argument, you need to set
- in application.yml:
params:
- <arg_name>: ?<param_name>e.g.:
endpoints:
- http-method: GET
bean-method: setValue
params:
value: ?val- or in application.properties:
<...>.params.<arg_name>=?<param_name>e.g.:
exposer.routes[0].endpoints[0].params.value=?valWithout question mark before the param value it will be set as a constant value for the argument
If you don't define the endpoint params, method params names will be used as request params
Full sample application.yml:
exposer:
bean: TestService2
routes:
- path: /test
routes:
- path: /v1
bean: TestService
endpoints:
- http-method: GET
bean-method: getValue
- http-method: POST
bean-method: setValue
- path: /v2
bean: TestService
endpoints:
- http-method: GET
bean-method: setValue
params:
value: ?val
endpoints:
- http-method: GET
bean-method: joinTwoArgs
params:
arg1: ?val
arg2: tstFull sample application.properties:
exposer.bean=TestService2
exposer.routes[0].path=/test
exposer.routes[0].routes[0].path=/v1
exposer.routes[0].routes[0].bean=TestService
exposer.routes[0].routes[0].endpoints[0].http-method=GET
exposer.routes[0].routes[0].endpoints[0].bean-method=getValue
exposer.routes[0].routes[0].endpoints[1].http-method=POST
exposer.routes[0].routes[0].endpoints[1].bean-method=setValue
exposer.routes[0].routes[1].path=/v2
exposer.routes[0].routes[1].bean=TestService
exposer.routes[0].routes[1].endpoints[0].http-method=GET
exposer.routes[0].routes[1].endpoints[0].bean-method=getValue
exposer.routes[0].routes[1].endpoints[0].params.value=?val
exposer.routes[0].endpoints[0].http-method=GET
exposer.routes[0].endpoints[0].bean-method=joinTwoArgs
exposer.routes[0].endpoints[0].params.arg1=?val
exposer.routes[0].endpoints[0].params.arg2=tst@Configuration
public class ExposerConfig implements ExposerConfigurer {
@Override
public ExposerConfiguration configureExposer() {
return ExposerConfiguration.builder()
.bean("TestService2")
.route("/test")
.route("/v1")
.bean("TestService")
.endpoint(RequestMethod.GET, "getValue").and()
.endpoint(RequestMethod.POST, "setValue").register()
.and()
.route("/v2")
.endpoint(RequestMethod.GET, "setValue")
.bean("TestService").param("value", "?val")
.register()
.and()
.endpoint(RequestMethod.GET, "joinTwoArgs")
.param("arg1", "?val").param("arg2", "tst")
.register()
.add()
.configure();
}
}See exposer-sample project in this repository
Sample config code, application.yml & application.properties belong to this project
(but defined for corresponding Spring profiles code, yml & prop)
The project is licensed under MIT License
Feel free to open issues and pull requests on GitHub. This is my first public project, so please don’t expect enterprise grade support.