Skip to content

Add spring boot starters and some restructuring #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
.flattened-pom.xml
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

# Eclipse m2e generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath

.settings

.factorypath
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ The client is available in the [Maven Central Repository](https://mvnrepository.
</dependency>
```

### Spring Boot Starter
If you are using Spring Boot, you can instead use the starter, which includes the api-client itself and an AutoConfiguration to create a RecombeeClient Bean. Add the following `<dependency>` entry to your project's POM:

```xml
<dependency>
<groupId>com.recombee</groupId>
<artifactId>api-client-spring-boot-2-starter</artifactId>
<version>5.0.0</version>
</dependency>
```
For the automatic configuration of the bean, you need to add the following properties:

```yaml
com.recombee.client.database-id=my-database-id
com.recombee.client.token=db-private-token
com.recombee.client.region=US_WEST
```

## Examples

### Basic example
Expand All @@ -39,6 +57,8 @@ import java.util.Random;
public class BasicExample {
public static void main(String[] args) {

// Create an instance of the client manually. If you are using Spring Boot with the starter,
// you can use dependency injection to get a fully configured instance of the RecombeeClient.
RecombeeClient client = new RecombeeClient("--my-database-id--", "--db-private-token--").setRegion(Region.US_WEST);
try {

Expand Down
56 changes: 56 additions & 0 deletions api-client-spring-boot-2-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.recombee</groupId>
<artifactId>api-client-parent</artifactId>
<version>${revision}</version>
</parent>
<artifactId>api-client-spring-boot-2-autoconfigure</artifactId>
<name>Recombee API Client Spring Boot 2 Autoconfiguration</name>

<properties>
<dependency.springboot.version>${spring.boot.2.version}</dependency.springboot.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${dependency.springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>com.recombee</groupId>
<artifactId>api-client</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.recombee.api_client.spring.boot._2.autoconfiguration;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

import com.recombee.api_client.RecombeeClient;

@AutoConfiguration
@ConditionalOnClass(RecombeeClient.class)
@ConditionalOnProperty(prefix = RecombeeClientAutoconfiguration.DEFAULT_RECOMBEE_CLIENT_PROPERTIES_PREFIX, name = "enabled", matchIfMissing = true)
public class RecombeeClientAutoconfiguration {

public static final String DEFAULT_RECOMBEE_CLIENT_PROPERTIES_PREFIX = "com.recombee.client";

@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(DEFAULT_RECOMBEE_CLIENT_PROPERTIES_PREFIX)
RecombeeClientProperties recombeeClientProperties() {
return new RecombeeClientProperties();
}

@Bean
@ConditionalOnMissingBean
RecombeeClient recombeeClient(RecombeeClientProperties recombeeClientProperties) {
return new RecombeeClient(recombeeClientProperties.getDatabaseId(), recombeeClientProperties.getToken())
.setRegion(recombeeClientProperties.getRegion());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.recombee.api_client.spring.boot._2.autoconfiguration;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;

import com.recombee.api_client.util.Region;

@Validated
public class RecombeeClientProperties {
@NotBlank
private String databaseId;
@NotBlank
private String token;
@NotNull
private Region region;

public RecombeeClientProperties() {
// empty no-args constructor
}

public RecombeeClientProperties(@NotBlank String databaseId, @NotBlank String token, @NotNull Region region) {
this.databaseId = databaseId;
this.token = token;
this.region = region;
}

public String getDatabaseId() {
return databaseId;
}

public void setDatabaseId(String databaseId) {
this.databaseId = databaseId;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public Region getRegion() {
return region;
}

public void setRegion(Region region) {
this.region = region;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.recombee.api_client.spring.boot._2.autoconfiguration.RecombeeClientAutoconfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.recombee.api_client.spring.boot._2.autoconfiguration.RecombeeClientAutoconfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.recombee.api_client.spring.boot._2.autoconfiguration;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ActiveProfiles;

import com.recombee.api_client.RecombeeClient;
import com.recombee.api_client.spring.boot._2.autoconfiguration.RecombeeClientAutoconfigurationTest.TestConfig;

@SpringBootTest(classes = TestConfig.class)
@ActiveProfiles("test")
public class RecombeeClientAutoconfigurationTest {

@Configuration
@EnableAutoConfiguration
public static class TestConfig {
}

@Autowired
private RecombeeClient client;

@Test
public void testAutoConfigurationExecutes() {
assertThat(client).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
com.recombee.client.database-id=my-database-id
com.recombee.client.token=db-private-token
com.recombee.client.region=US_WEST
24 changes: 24 additions & 0 deletions api-client-spring-boot-2-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.recombee</groupId>
<artifactId>api-client-parent</artifactId>
<version>${revision}</version>
</parent>
<artifactId>api-client-spring-boot-2-starter</artifactId>
<name>Recombee API Client Spring Boot 2 Starter</name>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>com.recombee</groupId>
<artifactId>api-client-spring-boot-2-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>com.recombee</groupId>
<artifactId>api-client</artifactId>
</dependency>
</dependencies>
</project>
56 changes: 56 additions & 0 deletions api-client-spring-boot-3-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.recombee</groupId>
<artifactId>api-client-parent</artifactId>
<version>${revision}</version>
</parent>
<artifactId>api-client-spring-boot-3-autoconfigure</artifactId>
<name>Recombee API Client Spring Boot 3 Autoconfiguration</name>

<properties>
<dependency.springboot.version>${spring.boot.3.version}</dependency.springboot.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${dependency.springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>com.recombee</groupId>
<artifactId>api-client</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.recombee.api_client.spring.boot._3.autoconfiguration;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

import com.recombee.api_client.RecombeeClient;

@AutoConfiguration
@ConditionalOnClass(RecombeeClient.class)
@ConditionalOnProperty(prefix = RecombeeClientAutoconfiguration.DEFAULT_RECOMBEE_CLIENT_PROPERTIES_PREFIX, name = "enabled", matchIfMissing = true)
public class RecombeeClientAutoconfiguration {

public static final String DEFAULT_RECOMBEE_CLIENT_PROPERTIES_PREFIX = "com.recombee.client";

@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(DEFAULT_RECOMBEE_CLIENT_PROPERTIES_PREFIX)
RecombeeClientProperties recombeeClientProperties() {
return new RecombeeClientProperties();
}

@Bean
@ConditionalOnMissingBean
RecombeeClient recombeeClient(RecombeeClientProperties recombeeClientProperties) {
return new RecombeeClient(recombeeClientProperties.getDatabaseId(), recombeeClientProperties.getToken())
.setRegion(recombeeClientProperties.getRegion());
}

}
Loading