mls
mls

Reputation: 1

Spring Data JPA projection interface not working with WebTestClient

I have an projection interface:

public interface BloodUnits {
    BloodType getBloodType();
    Integer getTotalUnits();
}

and a method in a repository:

@Repository
public interface BloodDonationsRepository extends JpaRepository<BloodDonations, Long> {
    @Query(value = "SELECT u.bloodType AS bloodType, SUM(bd.units) AS totalUnits "
            + "FROM blood_donations bd, users u WHERE bd.donor.id = u.id GROUP BY u.bloodType")
    List<BloodUnits> countAvailableUnitsByBloodType();
}

when i call this method via Postman it works as expected but when i call this method via WebTestClient:

@SpringBootTest(webEnvironment = RANDOM_PORT)
public class BloodDonationsControllerTest {

    @Autowired
    WebTestClient webTestClient;
.
.
.
List<BloodUnits> availableBloodUnits = webTestClient.get()
                .uri(bloodDonationURI + "/admin/available_blood_units")
                .accept(MediaType.APPLICATION_JSON)
                .headers(h -> h.setBearerAuth(jwt))
                .exchange()
                .expectStatus()
                .isOk()
                .expectBodyList(new ParameterizedTypeReference<BloodUnits>(){})
                .returnResult()
                .getResponseBody();
.
.
.
}

it throws an error: org.springframework.core.codec.CodecException: Type definition error: [simple type, class com.bds.dto.BloodUnits] . . . Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com.bds.dto.BloodUnits (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information...

Do you have any idea what is causing this problem and how to solve it? I tried to move the location of interface to entity folder but it didnt help. As I understand how it should work, Spring creates a proxy instance of the projection interface and all calls to the proxy are forwarded to that object. So im confused if it works with Postman, why it is not working with WebTestClient simulating Postman? If Spring is creating instance for Postman why it is not creating for this testing case?

Upvotes: 0

Views: 76

Answers (0)

Related Questions