Estou fazendo uma pesquisa passando parametros utilizando o especification do JPA, porem a consulta findAll
executa retornando toda a listagem sem filtrar pelos parametros.
o interessante é que se eu usar o Spring Boot 2.7.4 funciona perfeitamente da maneira que vou enviar aqui, porém preciso usar o Spring Boot e Spring Data JPA 3.0.1.
Segue o projeto:
Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rader</groupId>
<artifactId>algafood-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>algafood-api</name>
<description>API de delivery de comida</description>
<properties>
<java.version>19</java.version>
<dockerfile-maven-version>1.4.13</dockerfile-maven-version>
<modelmapper.version>3.1.0</modelmapper.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- Starters -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- Ferramentas -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- Banco de dados e JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- Flyway -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
<!--Testes -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<!-- Model Mapper-->
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>${modelmapper.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>docker</id>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>algafood-api</repository>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
Classe Model
@Setter
@Getter
public class PedidoResumoModel {
private String codigo;
private BigDecimal subtotal;
private BigDecimal taxaFrete;
private BigDecimal valorTotal;
private String status;
private OffsetDateTime dataCriacao;
private RestauranteResumoModel restaurante;
private UsuarioModel cliente;
}
Controlller PedidoController.java
@GetMapping
public List<PedidoResumoModel> pesquisar(PedidoFilter filtro) {
List<Pedido> todosPedidos = pedidoRepository.findAll(PedidoSpecs.usandoFiltro(filtro));
return pedidoResumoModelAssembler.toCollectionModel(todosPedidos);
}
PedidoSpecs.java
ublic class PedidoSpecs {
public static Specification<Pedido> usandoFiltro(PedidoFilter filtro) {
return (root, query, builder) -> {
root.fetch("restaurante").fetch("cozinha");
root.fetch("cliente");
var predicates = new ArrayList<Predicate>();
if (filtro.getClienteId() != null) {
predicates.add(builder.equal(root.get("cliente"), filtro.getClienteId()));
}
if (filtro.getRestauranteId() != null) {
predicates.add(builder.equal(root.get("restaurante"), filtro.getRestauranteId()));
}
if (filtro.getDataCriacaoInicio() != null) {
predicates.add(builder.greaterThanOrEqualTo(root.get("dataCriacao"),
filtro.getDataCriacaoInicio()));
}
if (filtro.getDataCriacaoFim() != null) {
predicates.add(builder.lessThanOrEqualTo(root.get("dataCriacao"),
filtro.getDataCriacaoFim()));
}
return builder.and(predicates.toArray(new Predicate[0]));
};
}
}
Repositorio
@Repository
public interface PedidoRepository extends JpaSpecificationExecutor<Pedido>, CustomJpaRepository<Pedido, Long>{
Optional<Pedido> findByCodigo(String codigo);
@Query("from Pedido p join fetch p.cliente join fetch p.restaurante r join fetch r.cozinha")
List<Pedido> findAll(Specification<Pedido> spec);
}
Requisiçao:
GET
- /pedidos?clienteId=2&restauranteId=2
Imagem do Postman com a requisiçao
e retorna a lista completa sem o filtro
[
{
"codigo": "f9981ca4-5a5e-4da3-af04-933861df3e55",
"subtotal": 298.90,
"taxaFrete": 10.00,
"valorTotal": 308.90,
"status": "CRIADO",
"dataCriacao": "2023-01-30T06:34:21Z",
"restaurante": {
"id": 1,
"nome": "Thai Gourmet"
},
"cliente": {
"id": 1,
"nome": "João da Silva",
"email": "[email protected]"
}
},
{
"codigo": "d178b637-a785-4768-a3cb-aa1ce5a8cdab",
"subtotal": 79.00,
"taxaFrete": 0.00,
"valorTotal": 79.00,
"status": "CRIADO",
"dataCriacao": "2023-01-30T06:34:21Z",
"restaurante": {
"id": 4,
"nome": "Java Steakhouse"
},
"cliente": {
"id": 1,
"nome": "João da Silva",
"email": "[email protected]"
}
},
{
"codigo": "b5741512-8fbc-47fa-9ac1-b530354fc0ff",
"subtotal": 110.00,
"taxaFrete": 10.00,
"valorTotal": 120.00,
"status": "ENTREGUE",
"dataCriacao": "2019-10-30T21:10:00Z",
"restaurante": {
"id": 1,
"nome": "Thai Gourmet"
},
"cliente": {
"id": 1,
"nome": "João da Silva",
"email": "[email protected]"
}
},
{
"codigo": "5c621c9a-ba61-4454-8631-8aabefe58dc2",
"subtotal": 174.40,
"taxaFrete": 5.00,
"valorTotal": 179.40,
"status": "ENTREGUE",
"dataCriacao": "2019-11-02T20:34:04Z",
"restaurante": {
"id": 1,
"nome": "Thai Gourmet"
},
"cliente": {
"id": 2,
"nome": "Maria Joaquina",
"email": "[email protected]"
}
},
{
"codigo": "8d774bcf-b238-42f3-aef1-5fb388754d63",
"subtotal": 87.20,
"taxaFrete": 10.00,
"valorTotal": 97.20,
"status": "ENTREGUE",
"dataCriacao": "2019-11-02T21:00:30Z",
"restaurante": {
"id": 1,
"nome": "Thai Gourmet"
},
"cliente": {
"id": 3,
"nome": "José Souza",
"email": "[email protected]"
}
}
]
Segue o link do repositório do código de toda a implementaçao: