首页 > 解决方案 > AWS lambda 使用 RestEasy 执行 http 请求:找不到内容类型应用程序/json 类型的编写器

问题描述

我目前正在使用无服务器 lambda 开发 Alexa 技能。我的技能执行一个 http 请求来触发一些动作。

为此,我正在使用 RestEasy。我的问题是在 aws lambda 中执行请求时总是收到异常。从我的 IDE 中执行(测试的小主要方法)。也许 IDE 正在获取一些额外的东西。

 Caused by: javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request: javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type application/json type: at.tsul.dev.f4l.uni.asterics.astericsAlexaFunction.client.AlexaRequestJson
    at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.invoke(ManualClosingApacheHttpClient43Engine.java:297)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:492)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:67)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.post(ClientInvocationBuilder.java:226)
    at at.tsul.dev.f4l.uni.asterics.astericsAlexaFunction.client.AstericsClient.performRequest(AstericsClient.java:29)
    at at.tsul.dev.f4l.uni.asterics.astericsAlexaFunction.handlers.MouseActionIntent.handle(MouseActionIntent.java:25)
    at at.tsul.dev.f4l.uni.asterics.astericsAlexaFunction.handlers.MouseActionIntent.handle(MouseActionIntent.java:12)
    at com.amazon.ask.request.handler.adapter.impl.BaseHandlerAdapter.execute(BaseHandlerAdapter.java:55)
    at com.amazon.ask.request.dispatcher.impl.BaseRequestDispatcher.doDispatch(BaseRequestDispatcher.java:177)
    at com.amazon.ask.request.dispatcher.impl.BaseRequestDispatcher.dispatch(BaseRequestDispatcher.java:110)
    ... 8 more
Caused by: javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type application/json type: at.tsul.dev.f4l.uni.asterics.astericsAlexaFunction.client.AlexaRequestJson
    at org.jboss.resteasy.core.interception.jaxrs.ClientWriterInterceptorContext.throwWriterNotFoundException(ClientWriterInterceptorContext.java:50)
    at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.getWriter(AbstractWriterInterceptorContext.java:302)
    at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.syncProceed(AbstractWriterInterceptorContext.java:240)
    at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:224)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.writeRequestBody(ClientInvocation.java:444)
    at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.writeRequestBodyToOutputStream(ManualClosingApacheHttpClient43Engine.java:601)
    at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.buildEntity(ManualClosingApacheHttpClient43Engine.java:560)
    at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.loadHttpMethod(ManualClosingApacheHttpClient43Engine.java:465)
    at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.invoke(ManualClosingApacheHttpClient43Engine.java:275)
    ... 17 more

我有以下依赖项:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-bom</artifactId>
                <version>4.6.0.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>com.amazon.alexa</groupId>
            <artifactId>ask-sdk</artifactId>
            <version>2.37.1</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson2-provider</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
        </dependency>
    </dependencies>

在依赖关系树中,我看不到任何版本不匹配或类似情况,可能是因为我正在使用 resteasy-bom。

这是执行请求的代码:

 public static AlexaResponseJson performRequest (AlexaRequestJson json) {
        ResteasyClient client = new ResteasyClientBuilderImpl()//
                .connectTimeout(1000, TimeUnit.MILLISECONDS)//
                .readTimeout(1000, TimeUnit.MILLISECONDS)//
                .build();

        String url = System.getenv("astericsUrl");
        ResteasyWebTarget target = client.target(url);

        AstericsEndpoint asterics = target.proxy(AstericsEndpoint.class);
        AlexaResponseJson response = asterics.triggerAction(json);

        client.close();
        return response;
    }

*Json 类只是普通的 POJO。而 AstericsEndpoint 类只定义了端点(EDIT1:定义 url、POST 方法和使用/生成应用程序/json)。

所以我的问题是,我错过了什么吗?尽管从同一工作区中的主方法直接调用时它仍然有效,但是否仍然缺少依赖项。还是我做错了导出(程序集插件:带有 jar-with-dependencies 描述符参考的单一目标)。我直接在生成的 jar 中检查了,那里有所有必需的依赖项。最好的问候

EDIT2:我已经在这里尝试并查看了不同的问题,还有这个:RESTEasy: Could not find writer for content-type application/json type。我可能是错的,但我认为这是一个稍微不同的问题,因为 json 创建和解析有效,但不适用于 AWS lambda 环境。

标签: javaamazon-web-servicesresteasy

解决方案


推荐阅读