Bakul Brahmbhatt
Bakul Brahmbhatt

Reputation: 61

How to call Multiple REST Client from Quarkus Controller?

Does Quarkus controller allow to use multiple rest clients? I pretty much tried everything in my power to figure out what I might be doing wrong but No success.

I am using below variables to point to open jdk 11 JAVA_HOME and JDK_HOME ( openjdk-11.0.2_windows-x64_bin ) graal vm GRAALVM_HOME ( graalvm-ce-java11-22.3.0 ) maven ( Apache Maven 3.8.6 )

Build works fine when running it using below commands mvn clean mvn install mvnw quarkus:dev

Issue happens in controller.
// IT BLOWS UP ON BlockHeaderController in method public BlockHeaderDTO addBlockHeader(BlockHeaderDTO dto) throws Exception

If I comment out this call to blockTypeCategoryService, then all services defined in controller works. I am not sure why controller allows to use Client: masterCustomerSetService but it is not liking client: blockTypeCategoryService

 /*  IF commented out this code then service works fine. 
    Optional<BlockTypeCategoryDTO> blockTypeRec =
            blockTypeCategoryService.getBlockTypeByCategoryAndType(
                    inputEntity.getBlockTypeCode(), "Costshare");
    if (blockTypeRec.isPresent()) {
        inputEntity.setBlockTypeCategoryId(blockTypeRec.get().getBlockTypeCategoryId());
    }
    */

I pretty much tried all tricks Trick 1 : changing scope of clients in properties file (ApplicationScope, Singleton, SessionScope ) all 3 doesn't work! Trick 2 : Using jandex-maven-plugin as described here : https://quarkus.io/guides/cdi-reference ( in addition to modifying application.properties with quarkus.arc.unremovable-types=demo.rest.clients.* )

**BlockHeaderController.java**   This is main Controller which use two clients 
BlockHeaderDTO.java
**MasterCustomerSetService.java**  This client works just fine! 
CustomerSet.java
**BlockTypeCategoryService.java**  This client gives problem for some reason 
BlockTypeCategoryDTO.java
BlockHeaderMapperImpl.java
BlockHeaderMapper.java
BlockHeaderRepository.java
BlockHeader.java
application.properties
pom.xml

All Code can be found here! 
https://drive.google.com/drive/folders/1xtJD8nFtFZAuts2hRQJ2HvpA3JcKwql7?usp=share_link

------------ Here is controller code ---


package demo.rest.controllers;

import demo.database.BlockHeader;
import demo.database.BlockHeaderRepository;
import demo.mapper.BlockHeaderMapper;
import demo.rest.clients.BlockTypeCategoryDTO;
import demo.rest.clients.BlockTypeCategoryService;
import demo.rest.clients.CustomerSet;
import demo.rest.clients.MasterCustomerSetService;

import io.quarkus.security.Authenticated;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;

@org.springframework.web.bind.annotation.RestController
@RequestMapping(value = "/quarkus/benefit/api/costshare",
                produces = MediaType.APPLICATION_JSON_VALUE)
@Authenticated
public class BlockHeaderController {
    @Inject
    BlockHeaderRepository blockHeaderRepository;

    @Inject
    BlockHeaderMapper blockHeaderMapper;

    @Inject
    @RestClient
    MasterCustomerSetService masterCustomerSetService;

    @Inject
    @RestClient
    BlockTypeCategoryService blockTypeCategoryService;

    @GetMapping("/masterCustomerSetService")
    public List<CustomerSet> getMasterCustomerSetByTypeAndResource(@RequestParam("type") String customerSetType) {
        return masterCustomerSetService
                .getMasterCustomerSetByTypeAndResource(customerSetType);
    }

    @GetMapping("/blockTypeCategoryService")
    public List<BlockTypeCategoryDTO> getAllBlockTypes() {
        return blockTypeCategoryService.getAllBlockTypes();
    }

    @PostMapping("/blockheader")
    public BlockHeaderDTO addBlockHeader(BlockHeaderDTO dto) throws Exception {
        BlockHeader inputEntity = blockHeaderMapper.toEntity(dto);

        if (StringUtils.isNotEmpty(dto.getMasterCustomerSetLongNm())) {
            Optional<CustomerSet> customer =
                    masterCustomerSetService.getMasterCustomerSetByName(
                            dto.getMasterCustomerSetLongNm(), "master");
            if (customer.isPresent()) {
                inputEntity.setMasterCustomerSetId(customer.get().getCustomerSetId());
            }
        }

        OffsetDateTime now = OffsetDateTime.now();
        inputEntity.setProcessCreateTs(now);
        inputEntity.setProcessCreateByUserId("CreateUser");
        inputEntity.setProcessCreateByProgramId("default");

        // IT BLOWS UP ON  blockTypeCategoryService
        /*
        Optional<BlockTypeCategoryDTO> blockTypeRec =
                blockTypeCategoryService.getBlockTypeByCategoryAndType(
                        inputEntity.getBlockTypeCode(), "Costshare");
        if (blockTypeRec.isPresent()) {
            inputEntity.setBlockTypeCategoryId(blockTypeRec.get().getBlockTypeCategoryId());
        }

        return blockHeaderMapper.toBlockDTO(blockHeaderRepository.save(inputEntity));
        */

        // After commenting about above code using /* .. */  code works.
        // code doesn't faile when calling client1 : masterCustomerSetService.getMasterCustomerSetByName
        // Not sure why calling client2 : blockTypeCategoryService.getBlockTypeByCategoryAndType is problem
        return blockHeaderMapper.toBlockDTO(inputEntity);

    }

}


Upvotes: 0

Views: 806

Answers (1)

Bakul Brahmbhatt
Bakul Brahmbhatt

Reputation: 61

I solved it! Issue was with conflicting client Jars.

    <!-- REST Client -->
    <!--
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-rest-client</artifactId>
    </dependency>
    -->
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-rest-client-reactive-jackson</artifactId>
    </dependency>

Upvotes: 1

Related Questions