-
Notifications
You must be signed in to change notification settings - Fork 2
(feat) update project config #59
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Copy this file to .env and update with your actual values | ||
| # DO NOT commit .env file to git - it's already in .gitignore | ||
| # | ||
| # These environment variables are used for database connection | ||
| # and correspond to the Spring Boot 3.5+ Couchbase properties: | ||
| # - spring.couchbase.connection-string | ||
| # - spring.couchbase.username | ||
| # - spring.couchbase.password | ||
|
|
||
| DB_CONN_STR=couchbases://your-cluster-url.cloud.couchbase.com | ||
| DB_USERNAME=your-username | ||
| DB_PASSWORD=your-password |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| .env | ||
|
|
||
| .DS_Store | ||
| *.iml | ||
| logs/ | ||
|
|
||
| .gradle | ||
| build/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package org.couchbase.quickstart.springdata.config; | ||
|
|
||
| import io.github.cdimascio.dotenv.Dotenv; | ||
| import org.springframework.context.ApplicationContextInitializer; | ||
| import org.springframework.context.ConfigurableApplicationContext; | ||
| import org.springframework.core.env.ConfigurableEnvironment; | ||
| import org.springframework.core.env.MapPropertySource; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class DotEnvConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
|
|
||
| @Override | ||
| public void initialize(ConfigurableApplicationContext applicationContext) { | ||
| ConfigurableEnvironment environment = applicationContext.getEnvironment(); | ||
|
|
||
| try { | ||
| // Load .env file if it exists | ||
| Dotenv dotenv = Dotenv.configure() | ||
| .directory(".") | ||
| .ignoreIfMalformed() | ||
| .ignoreIfMissing() | ||
| .load(); | ||
|
|
||
| // Create a property source from .env entries | ||
| Map<String, Object> envMap = new HashMap<>(); | ||
| dotenv.entries().forEach(entry -> { | ||
| String key = entry.getKey(); | ||
| String value = entry.getValue(); | ||
|
|
||
| // Only add if not already set by system environment | ||
| if (System.getenv(key) == null) { | ||
| envMap.put(key, value); | ||
| System.out.println("Loaded from .env: " + key); | ||
| } | ||
| }); | ||
|
|
||
| if (!envMap.isEmpty()) { | ||
| environment.getPropertySources().addFirst(new MapPropertySource("dotenv", envMap)); | ||
| System.out.println("Environment variables loaded from .env file: " + envMap.keySet()); | ||
| } | ||
|
|
||
| } catch (Exception e) { | ||
| System.err.println("Could not load .env file: " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package org.couchbase.quickstart.springdata.models; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.SliceImpl; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
|
|
||
| public class RestResponseSlice<T> extends SliceImpl<T> { | ||
| @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
| public RestResponseSlice(@JsonProperty("content") List<T> content, | ||
| @JsonProperty("number") int number, | ||
| @JsonProperty("size") int size, | ||
| @JsonProperty("pageable") JsonNode pageable, | ||
| @JsonProperty("last") boolean last, | ||
| @JsonProperty("sort") JsonNode sort, | ||
| @JsonProperty("first") boolean first, | ||
| @JsonProperty("numberOfElements") int numberOfElements, | ||
| @JsonProperty("hasNext") boolean hasNext) { | ||
|
|
||
| super(content, PageRequest.of(number, size), hasNext); | ||
| } | ||
|
|
||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constructor currently accepts several properties that are part of a You will need to add the following import: @JsonIgnoreProperties(ignoreUnknown = true)
public class RestResponseSlice<T> extends SliceImpl<T> {
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public RestResponseSlice(@JsonProperty("content") List<T> content,
@JsonProperty("number") int number,
@JsonProperty("size") int size,
@JsonProperty("hasNext") boolean hasNext) {
super(content, PageRequest.of(number, size), hasNext);
}
} |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| org.springframework.context.ApplicationContextInitializer=org.couchbase.quickstart.springdata.config.DotEnvConfiguration |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,15 @@ | ||
| server.use-forward-headers=true | ||
| # Server configuration | ||
| server.forward-headers-strategy=framework | ||
| spring.couchbase.bootstrap-hosts=DB_CONN_STR | ||
| spring.couchbase.bucket.name=travel-sample | ||
| spring.couchbase.bucket.user=DB_USERNAME | ||
| spring.couchbase.bucket.password=DB_PASSWORD | ||
| spring.couchbase.scope.name=inventory | ||
| spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER | ||
|
|
||
| # Modern Couchbase configuration (Spring Boot 3.5+) | ||
| spring.couchbase.connection-string=${DB_CONN_STR} | ||
| spring.couchbase.username=${DB_USERNAME} | ||
| spring.couchbase.password=${DB_PASSWORD} | ||
|
|
||
| # Couchbase connection and query optimizations | ||
| spring.couchbase.env.timeouts.query=30000ms | ||
| spring.couchbase.env.timeouts.key-value=5000ms | ||
| spring.couchbase.env.timeouts.connect=10000ms | ||
|
|
||
| # Spring MVC configuration | ||
| spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better logging practices within a Spring application, it's recommended to use a proper logging framework like SLF4J instead of
System.out.printlnandSystem.err.println. This provides more control over log levels and output formats.Additionally, when catching exceptions, logging the full stack trace (
e) is more informative for debugging than just logging the message (e.getMessage()).To apply this suggestion, you'll also need to add a logger field to the class and the necessary imports: