micronaut-guides / micronaut-http-client

7 stars 7 forks source link

Error DefaultServletHttpRequest cannot be cast to class io.micronaut.http.MutableHttpRequest #11

Closed rafael-coelho-amaro closed 3 years ago

rafael-coelho-amaro commented 3 years ago

I am getting the error below when I they to proxy a API call:

22:34:47.841 [multithreadEventLoopGroup-1-1] WARN  i.n.util.concurrent.DefaultPromise - An exception was thrown by io.micronaut.http.client.netty.DefaultHttpClient$$Lambda$496/0x0000000840598840.operationComplete()
java.lang.ClassCastException: class io.micronaut.servlet.engine.DefaultServletHttpRequest cannot be cast to class io.micronaut.http.MutableHttpRequest (io.micronaut.servlet.engine.DefaultServletHttpRequest and io.micronaut.http.MutableHttpRequest are in unnamed module of loader 'app')
    at io.micronaut.http.client.netty.DefaultHttpClient.prepareRequest(DefaultHttpClient.java:2523)
    at io.micronaut.http.client.netty.DefaultHttpClient.streamRequestThroughChannel(DefaultHttpClient.java:1857)
    at io.micronaut.http.client.netty.DefaultHttpClient.lambda$null$49(DefaultHttpClient.java:2673)
    at io.micronaut.http.client.netty.DefaultHttpClient.lambda$addInstrumentedListener$52(DefaultHttpClient.java:2729)
    at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578)
    at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:571)
    at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:550)
    at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491)
    at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616)
    at io.netty.util.concurrent.DefaultPromise.setSuccess0(DefaultPromise.java:605)
    at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:104)
    at io.netty.channel.DefaultChannelPromise.trySuccess(DefaultChannelPromise.java:84)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:300)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:335)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)
DefaultServletHttpRequestDisconnected from the target VM, address: '127.0.0.1:57415', transport: 'socket'

My code is:

package com.rafael.gateway.controllers

import com.rafael.configs.GatewayConfig
import io.micronaut.http.HttpRequest
import io.micronaut.http.MutableHttpResponse
import io.micronaut.http.annotation.Filter
import io.micronaut.http.client.netty.DefaultHttpClient
import io.micronaut.http.filter.OncePerRequestHttpServerFilter
import io.micronaut.http.filter.ServerFilterChain
import org.reactivestreams.Publisher
import org.slf4j.LoggerFactory
import java.net.URL

@Filter("/fromeverycall/**")
class ProxyFilter(val gatewayConfig: GatewayConfig) : OncePerRequestHttpServerFilter() {
    private val LOG = LoggerFactory.getLogger(ProxyFilter::class.java)

    private val client : DefaultHttpClient = DefaultHttpClient(
            URL("https://%s/".replace("%s", gatewayConfig.urn!!))
    )

    override fun doFilterOnce(request: HttpRequest<*>?, chain: ServerFilterChain?): Publisher<MutableHttpResponse<*>> {
        return client.proxy(request)
    }
}

The error occurs when the command client.proxy(request) is executed.

ilopmar commented 3 years ago

We are in the middle of migrating all the guides to the new infrastructure. See this for more information: https://micronaut.io/2021/04/12/improving-the-micronaut-guides-infrastructure/

Please take a look at the updated version of the guide: https://micronaut-projects.github.io/micronaut-guides-poc/latest/micronaut-http-client.html

rafael-coelho-amaro commented 3 years ago

Good morning,

So, I need to proxy the role request and then return role response using this Filter. Anyway I try I get the same error, like:

    override fun doFilterOnce(request: HttpRequest<*>?,
                              chain: ServerFilterChain?):
            Publisher<MutableHttpResponse<*>> {
        var resp = client.exchange(request).blockingFirst()
        return Publishers.just(HttpResponse.ok(String))
    }

I Looked at the links you sent, but I did not find anything that I can use. Because I need to redirect role Request and Response, not just the body.

*in this case, the command "client.exchange(request).blockingFirst()" return the same casting error

Could you help me?

ilopmar commented 3 years ago

You opened the issue on this guide and I linked you to the new guide. If you are looking for other thing, please ask the question on our Gitter Channel: https://gitter.im/micronautfw/

Thanks!