Search code examples
spring-bootcouchbasespring-data-couchbase

Spring Boot and Couchbase Connection Error


I am starting to learn spring boot and couchbase together, and implementing a simple custom query. However when I hit localhost:8889/agents/findByAgentId/14045, I got "this site can't be reached" error. What I missed here? I will appreciate any response. Thank you

here is the Entity Class

import com.sun.istack.internal.NotNull;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.couchbase.core.mapping.Field;

@Document
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Agent {

@Id
@NotNull
@Field
private String AgentLeaderId;

@Field
private String agentPreference;

@Field
private String agency;

@Field
private String mobilePhone;

}

Here is the Configuration Class

import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;

@Configuration
public class AgentConfig extends AbstractCouchbaseConfiguration {

@Override
public String getConnectionString() {
    return ("127.0.0.1");
}

@Override
public String getUserName() {
    return "******";
}

@Override
public String getPassword() {
    return "*******";
}

@Override
public String getBucketName() {
    return "******";
}
}

Here is the Repository

import com.bit.pruleads.entity.Agent;
import org.springframework.data.couchbase.core.query.N1qlPrimaryIndexed;
import org.springframework.data.couchbase.core.query.ViewIndexed;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
import org.springframework.data.couchbase.repository.Query;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;

@Repository
@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "primaryLeadsData")
public interface AgentRepository extends CouchbaseRepository<Agent, String > {

    @Query("#{#n1ql.selectEntity} WHERE agentLeaderId = $1 AND #{#n1ql.filter}")
    List<Agent> findByAgentId(String agentId);

    @Query("#{#n1ql.selectEntity} WHERE mobilePhone = $1 AND #{#n1ql.filter}")
    List<Agent> findAgentsByPhoneNumber(String phoneNumber);

}

Controller Class

import com.bit.pruleads.entity.Agent;
import com.bit.pruleads.repository.AgentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AgentController {

@Autowired
private AgentRepository agentRepository;

public AgentController(AgentRepository agentRepository) {
    this.agentRepository = agentRepository;
}

@PostMapping("/findByAgentId/{id}")
public List<Agent> findByAgentId(@PathVariable String id) {
    return agentRepository.findByAgentId(id);
}

@PostMapping("/findAgentsByPhoneNumber/{phoneNumber}")
public List<Agent> findAgentByPhoneNumber(@PathVariable String phoneNumber) {
    return agentRepository.findAgentsByPhoneNumber(phoneNumber);
}

}

Here is the pom file

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-couchbase</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

Here is the Application properties

#Server port
server.port =8889

Here is some of the Logs

No active profile set, falling back to default profiles: default
Bootstrapping Spring Data Couchbase repositories in DEFAULT mode.
Finished Spring Data repository scanning in 456ms. 
Found 0 Couchbase 
repository interfaces.
Bootstrapping Spring Data Couchbase repositories in DEFAULT mode.
Finished Spring Data repository scanning in 35ms. 
Found 1 Couchbase repository interfaces.
Opened bucket "leads-data" 
Started PruleadsApplication in 7.502 seconds (JVM running for 8.685)
Closed bucket "leads-data" 
Node disconnected 
Completed shutdown and closed all open buckets 
Process finished with exit code 0

Solution

  • I think you need to have spring-boot-starter-web included in your pom. With no web server, spring-boot starts and finds it has nothing to do so it will just stop.