我正在使用 WebFlux 构建 Java SpringBoot 应用程序。 因此,在这个应用程序中,我需要制作一些 API 来使用 r2dbc 从数据库 TSQL 获取数据。
所以我创建了这段代码,这是我的界面存储库:
import it.silver.data.pojo.Percorsi;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;
public interface PercorsiRepository extends ReactiveCrudRepository<Percorsi, Integer> {
@Query("SELECT * FROM PRC_Percorsi")
Flux<Percorsi> getRepository();
}
这是我的 Percorsi 类映射表:
import lombok.*;
import org.apache.james.mime4j.dom.datetime.DateTime;
import org.springframework.data.annotation.Id;
@Data
@ToString
@Builder
@AllArgsConstructor
@Table(schema = "PRC_Percorsi")
public class Percorsi {
@Id
@Column(name = "ID")
private Integer id;
@Column(name = "Nome")
private String nome;
@Column(name = "Descrizione")
private String descrizione;
@Column(name = "IDPaese")
private Integer idPaese;
@Column(name = "IDRegione")
private Integer idRegione;
@Column(name = "IDProvincia")
private Integer idProvincia;
@Column(name = "IDComune")
private Integer idComune;
@Column(name = "Distanza")
private Double distanza;
@Column(name = "IDLivelloDifficolta")
private Integer idLivelloDifficolta;
@Column(name = "AltitudineMax")
private Double altitudineMax;
@Column(name = "AltitudineMin")
private Double altitudineMin;
@Column(name = "IDTipoPercorso")
private Integer idTipoPercorso;
@Column(name = "TempoMovimento")
private Integer tempoMovimento;
@Column(name = "TempoTotale")
private Integer tempoTotale;
@Column(name = "Dislivello")
private Integer dislivello;
@Column(name = "NVisualizzazioni")
private Integer nVisualizzazioni;
@Column(name = "NVisitatori")
private Integer nVisitatori;
@Column(name = "MapName")
private String mapName;
@Column(name = "MapPath")
private String mapPath;
@Column(name = "CreationUserId")
private Integer creationUserId;
@Column(name = "CreationDate")
private DateTime creationDate;
@Column(name = "LastChangeUserId")
private Integer lastChangeUserId;
@Column(name = "LastChangeDate")
private DateTime lastChangeDate;
@Column(name = "FileStateID")
private Integer fileStateId;
}
这是我的 Handler 类,它公开了要外部调用的方法:
import lombok.extern.slf4j.Slf4j;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UsersResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.util.RouteMatcher;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
@Slf4j
@Component
public class SilverMountainHandler extends AbstractTenantAwareHandler {
@Autowired
private KeycloakClientProvider clientProvider;
@Autowired
private KeycloakProperties keycloakProperties;
@Autowired
private ServiceProperties serviceProperties;
@Autowired
private ObjectMapper objectMapper;
private final PercorsiRepository percorsiRepository;
protected SilverMountainHandler(final ServiceProvider serviceProvider) {
super(serviceProvider);
}
private SilverMountainService silverMountainService(final UserInfo userInfo) {
return super.getService(userInfo, SilverMountainService.class);
}
public Mono<ServerResponse> routes(final ServerRequest request) {
Flux<Percorsi> people = this.percorsiRepository.findAll();
return people.collectList().flatMap(p->
p.size() < 1 ?
ServerResponse.status(404).build()
:ServerResponse.ok().contentType(APPLICATION_JSON).body(fromObject(p))
);
}
// endregion
}
我该如何修复它?
您可能忘记了 PercorsiRepository 接口中的注解 @Repository,因为该注解告诉 Spring 这是一个应该由 Spring 容器管理的组件。
@Repository
public interface PercorsiRepository extends ReactiveCrudRepository<Percorsi, Integer> {
@Query("SELECT * FROM PRC_Percorsi")
Flux<Percorsi> getRepository();
}