I’ve been looking to try out Spring Boot to make a quick REST API with Couchbase Spring Data for some tests.
For one particular part, I’d like to access the lower-level Couchbase API’s directly. Fortunately it seems that CouchbaseTemplate provides a getCouchbaseBucket() method for exactly this purpose.
My issue is that I can’t quite see how to wire everything up so that the CouchbaseTemplate is accessible from the right classes. Or, in fact, where would be the appropriate place to access the CouchbaseTemplate.
Currently I have several basic files:
Application, Greeting, GreetingController, GreetingRepository
Application implements getBootstrapHosts, getBucketName, getBucketPassword and has the main function which runs the SpringApplication:
@Configuration
@EnableAutoConfiguration
@EnableCouchbaseRepositories
@SpringBootApplication
public class Application extends AbstractCouchbaseConfiguration
The Greeting class is a basic entity with an ID and a field with accessor methods:
@Document
public class Greeting {
@Id
private final String id;
@Field
private final String name;
GreetingRepository is currently a blank interface which extends CouchbaseRepository:
interface GreetingRepository extends CouchbaseRepository<Greeting, String>
GreetingController has REST end points and GreetingRepository reference:
@RestController
public class GreetingController {
@Autowired
private GreetingRepository repository;
...
...
@Bean
@RequestMapping("/cbgreeting")
public Greeting cbgreeting() {
Going back to the original question, where is the appropriate place to access CouchbaseTemplate, and what class structure is best to wrap the calls in?
It seems SimpleCouchbaseRepository has references to CouchbaseTemplate whilst the CouchbaseRepository interface does not have the relevant methods.