Код: Выделить всё
Unable to create a ConnectionFactory for 'ConnectionFactoryOptions{options={driver=h2, protocol=mem, database=contentitem, options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE}}'. Available drivers: [ pool ]
Есть идеи, что здесь происходит?
Код: Выделить всё
plugins {
id 'org.springframework.boot' version '2.3.9.RELEASE'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.mycorp.service'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'io.r2dbc:r2dbc-h2'
runtimeOnly 'io.r2dbc:r2dbc-postgresql'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'io.projectreactor:reactor-test'
compile 'io.springfox:springfox-swagger2:3.0.0'
compile 'io.springfox:springfox-swagger-ui:3.0.0'
compile 'io.springfox:springfox-spring-webflux:3.0.0'
}
test {
useJUnitPlatform()
}
Код: Выделить всё
CREATE TABLE contentitem ( contentItemId INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, localizedName VARCHAR(100) NOT NULL);Я заполняю таблицу с помощью файла data.sql в том же каталоге:
Код: Выделить всё
INSERT INTO contentitem (contentItemId, localizedName) VALUES (0, 'Zero');
INSERT INTO contentitem (contentItemId, localizedName) VALUES (1, 'One');
INSERT INTO contentitem (contentItemId, localizedName) VALUES (2, 'Two');
< /code>
Я создал CustomConnectionFactoryInitialize для создания и заполнения базы данных: < /p>
@Configuration
public class CustomConnectionFactoryInitializer {
@Bean
public ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {
ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
initializer.setConnectionFactory(connectionFactory);
CompositeDatabasePopulator populator = new CompositeDatabasePopulator();
populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("schema.sql")));
initializer.setDatabasePopulator(populator);
return initializer;
}
}
< /code>
Я определил профиль «тест», используя в памяти H2 и сделал его активным в моем файле Application.yml: < /p>
spring:
profiles:
active: test
---
spring:
profiles: dev
r2dbc:
url: r2dbc:postgresql://localhost:5432/test
username: postgres
password: postgres
logging:
level:
org.springframework.data.r2dbc: Debug
---
spring:
profiles: test
r2dbc:
url: r2dbc:h2:mem:///contentitem?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
name: sa
password:
---
spring:
profiles: prod
r2dbc:
url: r2dbc:postgresql://localhost:5432/test
username: postgres
password: postgres
logging:
level:
org.springframework.data.r2dbc: Debug
< /code>
Моя модель Contentitem: < /p>
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table("contentitem")
public class ContentItem {
@Id
@Column("contentItemId")
private Integer contentItemId;
@Column("localizedName")
private String localizedName;
}
< /code>
my contentitemcontroller: < /p>
@RestController
@RequestMapping("/contentItems")
public class ContentItemController {
@Autowired
private ContentItemService contentItemService;
@GetMapping("/{contentItemId}")
public Mono getContentItemByUserId(@PathVariable Integer contentItemId){
Mono contentItem = contentItemService.getContentItemById(contentItemId);
return contentItem.map( u -> ResponseEntity.ok(u))
.defaultIfEmpty(ResponseEntity.notFound().build());
}
< /code>
my contentIteMservice: < /p>
@Service
@Slf4j
@Transactional
public class ContentItemService {
@Autowired
private ContentItemRepository contentItemRepository;
public Mono getContentItemById(Integer contentItemId){
return contentItemRepository.findByContentItemId(contentItemId);
}
}
< /code>
и мой contentitemrepository: < /p>
public interface ContentItemRepository extends ReactiveCrudRepository {
Mono findByContentItemId(Integer contentItemId);
}
Подробнее здесь: https://stackoverflow.com/questions/675 ... -boot-with