Issue
My connection config:
@Configuration
@EnableR2dbcRepositories
public class DatabaseConfiguration extends AbstractR2dbcConfiguration {
@Bean
public PostgresqlConnectionFactory connectionFactory() {
return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
.host("r2dbc:postgresql://mydb.alfa")
.port(5432)
.username("admin")
.database("postgres")
.password(password)
.build()
);
}
}
My repository:
public interface ReactiveOdometerRepository extends ReactiveCrudRepository<OdometerEntity, String> {
Flux<OdometerEntity> findTop1By(String name);
}
My entity:
@Setter
public class OdometerEntity {
@Getter
@Id
private String name;
@Getter
private int value;
}
When I try querying the DB, it fails:
@PostMapping(value = "/query", produces = MediaType.APPLICATION_JSON_VALUE)
public int get(@RequestBody RequestObject request) {
return odometerRepository.findTop1ByName(request.getName()).getValue();
Error:
io.r2dbc.postgresql.PostgresqlConnectionFactory$PostgresConnectionException:
Cannot connect to r2dbc:postgresql://mydb.alfa/<unresolved>:5432
Not sure what the unresolved
is doing in there.
How do I fix this error? thanks
Solution
Your host
variable is wrong. It should contain only the name or ip-address of the host not the full URL.
So instead of host("r2dbc:postgresql://mydb.alfa")
use host("mydb.alfa")
.
@Bean
public PostgresqlConnectionFactory connectionFactory() {
return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
.host("mydb.alfa")
.port(5432)
.username("admin")
.database("postgres")
.password(password)
.build()
);
}
Answered By - M. Deinum
Answer Checked By - David Marino (JavaFixing Volunteer)