HttpClient throws null pointer exception in my class which extends MicronautRequestHandler

381 Views Asked by At

I am getting NullPointerException for httpClient in the code mentioned below:

@Introspected
public class CustomEventHandler extends MicronautRequestHandler<Map<String, Object>, APIGatewayV2HTTPResponse> {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomEventHandler.class);

    @Inject
    @Client("/")
    public final RxHttpClient httpClient;

    @Override
    public APIGatewayV2HTTPResponse execute(Map<String, Object> input) {
        String result = httpClient.toBlocking()
                .retrieve(HttpRequest
                        .GET("http://test-api.com"));

        final TestDto testDto;
        try {
            testDto = new ObjectMapper().readValue(result, TestDto.class);
            return APIGatewayV2HTTPResponse.builder().withStatusCode(HttpStatus.SC_OK)
                    .withBody(testDto.toString())
                    .build();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return APIGatewayV2HTTPResponse.builder().withStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
                .build();
    }
}
1

There are 1 best solutions below

0
On

Check this example how to use the client on serverless functions

https://guides.micronaut.io/micronaut-function-aws-lambda/guide/index.html#writingTheApp