Converting request body error while using Feign to process incoming httpServletRequest

3.6k views Asked by At

There are two modules in my project and I wanna get an httpServletRequest in the web module and send it to the core module by using feign in order to do some process on this request in the core module.

Here are the steps respectively:

web module controller:

@RestController
@RequestMapping(value = ["/**"])
class WebController(val urlShortenerClient: UrlShortenerClient) {
    val logger: Logger = LoggerFactory.getLogger(this.javaClass)

    @GetMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
    fun getRedirectionUrl(httpServletRequest: HttpServletRequest): String {
        logger.info("received url: ${httpServletRequest.requestURL}")
        //*** this log appears and then error appears
        return urlShortenerClient.getRedirectionUrl(httpServletRequest)
    }
}

web module feign:

@FeignClient(value = "url-shortener",
        url = "http://localhost:8080")
interface UrlShortenerClient {
    @RequestMapping(value = ["/url-shortener"], produces = [MediaType.APPLICATION_JSON_VALUE])
    fun getRedirectionUrl(httpServletRequest: HttpServletRequest): String
}

core module controller:

@RestController
@RequestMapping(value = ["/url-shortener"])
class UrlShortenerController(private val urlShortenerService: UrlShortenerService) {
    private val logger: Logger = LoggerFactory.getLogger(this.javaClass)

    @GetMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
    fun getRedirectionUrl(httpRequest: HttpServletRequest): String {
        logger.info("received url in core controller: ${httpRequest.requestURL}")

        return urlShortenerService.createUrlShortener(httpRequest)
    }
}

but when I send a get request by postman I get the following error(Acctually when the feign client method is going to be called in web controller the error appears):

2020-10-12 14:04:41.921 ERROR 20484 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.codec.EncodeException: Error converting request body] with root cause

java.lang.StackOverflowError: null
    at java.base/sun.net.util.IPAddressUtil.checkHostString(IPAddressUtil.java:460) ~[na:na]
    at java.base/java.net.URLStreamHandler.setURL(URLStreamHandler.java:518) ~[na:na]
    at java.base/java.net.URLStreamHandler.parseURL(URLStreamHandler.java:312) ~[na:na]
    at java.base/sun.net.www.protocol.file.Handler.parseURL(Handler.java:67) ~[na:na]
    at java.base/java.net.URL.<init>(URL.java:674) ~[na:na]
    at java.base/java.net.URL.<init>(URL.java:541) ~[na:na]
    at java.base/jdk.internal.loader.URLClassPath$FileLoader.getResource(URLClassPath.java:1219) ~[na:na]
    at java.base/jdk.internal.loader.URLClassPath.getResource(URLClassPath.java:314) ~[na:na]
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:695) ~[na:na]
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621) ~[na:na]
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:773) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755) ~[jackson-databind-2.11.2.jar:2.11.2]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.11.2.jar:2.11.2]
0

There are 0 answers