Search code examples
javaspringimportcouchbaseapi-design

What import statements am I missing to get this class to connect this couchbase server with my REST API?


I'm trying to connect a local Couchbase server with a Spring Boot REST API I'm building. The code I'm using is from the bottom of in the usage section: http://projects.spring.io/spring-data-couchbase/#quick-start

I think I have everything configured, I just can't get this last class to function without errors. I believe it's all related to having proper import statements, which I've only been able to figure out a few. The "user" object is having issues instantiating, stating it's abstract. Several functions based off that are therefore throwing errors. Box and Point also seem to not be able to agree on an import statement. I can only get one of them fixed at a time.

Here is the code for the class, I don't think you'll need anything from other classes:

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.List;

@Service
public class MyService {

    private final UserRepository userRepository;

    @Autowired
    public MyService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public void doWork() {
        userRepository.deleteAll();

        User user = new User();
        user.setLastname("Jackson");
        user.setLocation(new Point(123, 456));

        user = userRepository.save(user);

        List<User> jacksonChildren =
                userRepository.findByLastNameAndAgeBetween("Jackson", 0, 18);

        List<User> jacksonFamily =
                userRepository.findByLastName("Jackson");

        //bounding box is lower-left, upper-right corners
        Box cityBounds = new Box(new Point(100, 100), new Point(150, 500));
        List<User> jacksonsInSomeCity =
                userRepository.findByLocationWithin(cityBounds);
    }
}

Solution

  • User entity needs to be defined in your project. The reference documentation has an example http://docs.spring.io/spring-data/couchbase/docs/2.1.6.RELEASE/reference/html/#basics. Here is a sample project using Couchbase in spring boot https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-couchbase.