Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.web.filter.ForwardedHeaderFilter;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
Expand All @@ -19,6 +22,7 @@
@Slf4j
@SpringBootApplication(exclude = SecurityAutoConfiguration.class, proxyBeanMethods = false)
@OpenAPIDefinition(info = @Info(title = "Quickstart in Couchbase with Spring Data", version = "2.0", description = "<html><body><h2>A quickstart API using Java and Spring Data with Couchbase and travel-sample data</h2><p>We have a visual representation of the API documentation using Swagger which allows you to interact with the API's endpoints directly through the browser. It provides a clear view of the API including endpoints, HTTP methods, request parameters, and response objects.</p><p>Click on an individual endpoint to expand it and see detailed information. This includes the endpoint's description, possible response status codes, and the request parameters it accepts.</p><p><strong>Trying Out the API</strong></p><p>You can try out an API by clicking on the \"Try it out\" button next to the endpoints.</p><ul><li><strong>Parameters:</strong> If an endpoint requires parameters, Swagger UI provides input boxes for you to fill in. This could include path parameters, query strings, headers, or the body of a POST/PUT request.</li><li><strong>Execution:</strong> Once you've inputted all the necessary parameters, you can click the \"Execute\" button to make a live API call. Swagger UI will send the request to the API and display the response directly in the documentation. This includes the response code, response headers, and response body.</li></ul><p><strong>Models</strong></p><p>Swagger documents the structure of request and response bodies using models. These models define the expected data structure using JSON schema and are extremely helpful in understanding what data to send and expect.</p><p>For details on the API, please check the tutorial on the Couchbase Developer Portal: <a href=\"https://developer.couchbase.com/tutorial-quickstart-java-spring-boot\">https://developer.couchbase.com/tutorial-quickstart-java-spring-boot</a></p></body></html>"))
@EnableCouchbaseRepositories(basePackages = "org.couchbase.quickstart.springdata.repository")
public class Application implements CommandLineRunner {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.couchbase.quickstart.springdata.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.couchbase.client.core.error.CouchbaseException;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.manager.bucket.BucketSettings;
import com.couchbase.client.java.manager.bucket.BucketType;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class CouchbaseConfig {

private final DBProperties dbProp;

public CouchbaseConfig(DBProperties dbProp) {
this.dbProp = dbProp;
}


/**
* NOTE: If connecting to Couchbase Capella, you must enable TLS.
* <p>
* The simplest way to enable TLS is to edit {@code application.properties}
* and make sure the {@code spring.couchbase.bootstrap-hosts} config property
* starts with "couchbases://" (note the final 's'), like this:
* <pre>
* spring.couchbase.bootstrap-hosts=couchbases://my-cluster.cloud.couchbase.com
* </pre>
* Alternatively, you can enable TLS by writing code to configure the cluster environment;
* see the commented-out code in this method for an example.
*/
@Bean(destroyMethod = "disconnect")
public Cluster getCouchbaseCluster() {
try {
log.debug("Connecting to Couchbase cluster at " + dbProp.getHostName());
return Cluster.connect(dbProp.getHostName(), dbProp.getUsername(), dbProp.getPassword());

// Here is an alternative version that enables TLS by configuring the cluster environment.
/* return Cluster.connect(
dbProp.getHostName(),
ClusterOptions.clusterOptions(dbProp.getUsername(), dbProp.getPassword())
.environment(env -> { // Configure cluster environment properties here
env.securityConfig().enableTls(true);

// If you're connecting to Capella, the SDK already knows which certificates to trust.
// When using TLS with non-Capella clusters, you must tell the SDK which certificates to trust.
env.securityConfig().trustCertificate(
Paths.get("/path/to/trusted-root-certificate.pem")
);
})
);

*/

} catch (CouchbaseException e) {
log.error("Could not connect to Couchbase cluster at " + dbProp.getHostName());
log.error("Please check the username (" + dbProp.getUsername() + ") and password (" + dbProp.getPassword() + ")");
throw e;
} catch (Exception e) {
log.error("Could not connect to Couchbase cluster at " + dbProp.getHostName());
throw e;
}

}

@Bean
public Bucket getCouchbaseBucket(Cluster cluster) {

try {
// Creates the cluster if it does not exist yet
if (!cluster.buckets().getAllBuckets().containsKey(dbProp.getBucketName())) {
cluster.buckets().createBucket(
BucketSettings.create(dbProp.getBucketName())
.bucketType(BucketType.COUCHBASE)
.minimumDurabilityLevel(DurabilityLevel.NONE)
.ramQuotaMB(128));
}
return cluster.bucket(dbProp.getBucketName());
} catch (Exception e) {
log.error("Could not connect to Couchbase bucket " + dbProp.getBucketName());
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
package org.couchbase.quickstart.springdata.config;
// package org.couchbase.quickstart.springdata.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
// import org.springframework.beans.factory.annotation.Value;
// import org.springframework.context.annotation.Bean;
// import org.springframework.context.annotation.Configuration;
// import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
// import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;

import com.couchbase.client.core.msg.kv.DurabilityLevel;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.manager.bucket.BucketSettings;
import com.couchbase.client.java.manager.bucket.BucketType;
// import com.couchbase.client.core.msg.kv.DurabilityLevel;
// import com.couchbase.client.java.Bucket;
// import com.couchbase.client.java.Cluster;
// import com.couchbase.client.java.manager.bucket.BucketSettings;
// import com.couchbase.client.java.manager.bucket.BucketType;

@Configuration
@EnableCouchbaseRepositories
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
// @Configuration
// @EnableCouchbaseRepositories
// public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {

@Value("${spring.couchbase.bootstrap-hosts}")
private String host;
// @Value("${spring.couchbase.bootstrap-hosts}")
// private String host;

@Value("${spring.couchbase.bucket.user}")
private String username;
// @Value("${spring.couchbase.bucket.user}")
// private String username;

@Value("${spring.couchbase.bucket.password}")
private String password;
// @Value("${spring.couchbase.bucket.password}")
// private String password;

@Value("${spring.couchbase.bucket.name}")
private String bucket;
// @Value("${spring.couchbase.bucket.name}")
// private String bucket;


@Override
public String getConnectionString() {
// To connect to capella:
// - with ssl certificate validation:
// return "couchbases://cb.jnym5s9gv4ealbe.cloud.couchbase.com"
// - without ssl validation:
// return "couchbases://cb.jnym5s9gv4ealbe.cloud.couchbase.com?tls=no_verify"
// (replace cb.jnym5s9gv4ealbe.cloud.couchbase.com with your Capella cluster
// address)
// @Override
// public String getConnectionString() {
// // To connect to capella:
// // - with ssl certificate validation:
// // return "couchbases://cb.jnym5s9gv4ealbe.cloud.couchbase.com"
// // - without ssl validation:
// // return "couchbases://cb.jnym5s9gv4ealbe.cloud.couchbase.com?tls=no_verify"
// // (replace cb.jnym5s9gv4ealbe.cloud.couchbase.com with your Capella cluster
// // address)

return host;
}
// return host;
// }

@Override
public String getUserName() {
return username;
}
// @Override
// public String getUserName() {
// return username;
// }

@Override
public String getPassword() {
return password;
}
// @Override
// public String getPassword() {
// return password;
// }

@Override
public String getBucketName() {
return bucket;
}
// @Override
// public String getBucketName() {
// return bucket;
// }

@Override
public String typeKey() {
return "type";
}
// @Override
// public String typeKey() {
// return "type";
// }

@Bean
public Bucket getCouchbaseBucket(Cluster cluster){
// verify that bucket exists
if (!cluster.buckets().getAllBuckets().containsKey(getBucketName())) {
// create the bucket if it doesn't
cluster.buckets().createBucket(
BucketSettings.create(getBucketName())
.bucketType(BucketType.COUCHBASE)
.minimumDurabilityLevel(DurabilityLevel.NONE)
.ramQuotaMB(128));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
return cluster.bucket(getBucketName());
}
}
// // @Bean
// // public Bucket getCouchbaseBucket(Cluster cluster){
// // // verify that bucket exists
// // if (!cluster.buckets().getAllBuckets().containsKey(getBucketName())) {
// // // create the bucket if it doesn't
// // cluster.buckets().createBucket(
// // BucketSettings.create(getBucketName())
// // .bucketType(BucketType.COUCHBASE)
// // .minimumDurabilityLevel(DurabilityLevel.NONE)
// // .ramQuotaMB(128));
// // try {
// // Thread.sleep(1000);
// // } catch (InterruptedException e) {
// // Thread.currentThread().interrupt();
// // e.printStackTrace();
// // }
// // }
// // return cluster.bucket(getBucketName());
// // }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.couchbase.quickstart.springdata.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import lombok.Getter;

@Configuration
@Getter
public class DBProperties {

@Value("${spring.couchbase.bootstrap-hosts}")
private String hostName;
@Value("${spring.couchbase.bucket.user}")
private String username;
@Value("${spring.couchbase.bucket.password}")
private String password;
@Value("${spring.couchbase.bucket.name}")
private String bucketName;

}
Original file line number Diff line number Diff line change
@@ -1,54 +1,26 @@
package org.couchbase.quickstart.springdata.services;


import java.util.Optional;

import org.couchbase.quickstart.springdata.models.Airline;
import org.couchbase.quickstart.springdata.repository.AirlineRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class AirlineService {

private final AirlineRepository airlineRepository;

public AirlineService(AirlineRepository airlineRepository) {
this.airlineRepository = airlineRepository;
}

public Page<Airline> getAllAirlines(Pageable pageable) {
return airlineRepository.findAll(pageable);
}
public interface AirlineService {

public Optional<Airline> getAirlineById(String id) {
return airlineRepository.findById(id);
}
Page<Airline> getAllAirlines(Pageable pageable);

public Airline saveAirline(Airline airline) {
return airlineRepository.save(airline);
}
Optional<Airline> getAirlineById(String id);

public void deleteAirline(String id) {
airlineRepository.deleteById(id);
}
Airline saveAirline(Airline airline);

public Airline createAirline(Airline airline) {
return airlineRepository.save(airline);
}
void deleteAirline(String id);

public Airline updateAirline(String id, Airline airline) {
airline.setId(id);
return airlineRepository.save(airline);
}
Airline createAirline(Airline airline);

public Page<Airline> findByCountry(String country, Pageable pageable) {
return airlineRepository.findByCountry(country,pageable);
}
Airline updateAirline(String id, Airline airline);

public Page<Airline> findByDestinationAirport(String destinationAirport, Pageable pageable) {
return airlineRepository.findByDestinationAirport(destinationAirport, pageable);
}
Page<Airline> findByCountry(String country, Pageable pageable);

Page<Airline> findByDestinationAirport(String destinationAirport, Pageable pageable);
}
Loading