2

How to set up a PostgreSQL database connection in r2dbc Spring boot project?

I have tried the below configuration, it connects to the database but it's not returning any values

@Configuration
@EnableR2dbcRepositories
public class DatabaseConfig extends AbstractR2dbcConfiguration {

    @Override
    public ConnectionFactory connectionFactory() {
        return ConnectionFactories.get("r2dbc:postgresql://localhost:5432/sample");
    }

    /*@Override
    public ConnectionFactory connectionFactory() {
        return ConnectionFactories.get(new PostgresqlConnectionFactory(
                PostgresqlConnectionConfiguration.builder()
                .host("localhost")
                .port(5432)
                .username("postgres")
                .password("thirumal")
                .database("sample")
                .build()););
    }*/

}

application.properties

spring.r2dbc.url=r2dbc:postgresql://localhost:5432/sample
spring.r2dbc.username=postgres
spring.r2dbc.password=thirumal
spring.r2dbc.pool.enabled=true

Model

@Data@NoArgsConstructor@AllArgsConstructor@Getter@Setter
@ToString
@Table("public.test")
public class Test implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 4205798689305488147L;

    @Id//@Column("id")
    private Long id;
    
    private String name;
    
}

Repository

public interface TestRepository extends ReactiveCrudRepository<Test, Long> {

}

REST CONTROLLER:

@GetMapping("/test")
public Mono<Test> test() {
   testRepository.findById(3L).subscribe(v->System.out.println("Value: " + v.toString()));
   return testRepository.findById(3L);
}

It prints the output in the console but in the JSON, I get only empty braces {}

What is the correct way to configure? Any other configuration is required?

piet.t
  • 11,718
  • 21
  • 43
  • 52
Thirumal
  • 8,280
  • 11
  • 53
  • 103

2 Answers2

3

I found the problem. It's Lombok library, I didn't install it in eclipse.

When I created the getter and setter method manually it worked.

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

Then, I set up the lombok and used @getter and @setter and it worked.

Thirumal
  • 8,280
  • 11
  • 53
  • 103
2

This configuration works for me, but I use the DatabaseClient instead of the R2dbcRepositories to query the data:

@Configuration
public class DatabaseConfiguration extends AbstractR2dbcConfiguration {

    @Override
    @Bean
    public ConnectionFactory connectionFactory() {

        return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
                .host("localhost")
                .port(5432)
                .username("username")
                .password("password")
                .database("mydb")
                .build());
    }

}

Then in the repository:

@Repository
public class MyRepository {

    @Autowired
    private DatabaseClient client;


    public Flux<String> getString() {
        ....
    }
}

UPDATE:

If it's connect to the database probably your configuration is right, can you share also the code used to get the data?

It's possible that you are getting the result as Mono or Flux, but not reading from it (try with subscribe()).

Mono<String> mono = db.getData();
mono.subscribe(value -> System.out.println(value));
doscio
  • 89
  • 3