首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >天蓝色中的Spring云函数抛出空指针异常

天蓝色中的Spring云函数抛出空指针异常
EN

Stack Overflow用户
提问于 2022-02-25 10:31:49
回答 2查看 743关注 0票数 0

我已经将spring云函数部署到azure函数中,app.The API在本地开发人员机器中运行得非常好。当部署到azure时,从postman/client调用时会抛出一个空指针异常。 Java运行时版本: 11函数扩展版本:~3

代码语言:javascript
复制
2022-02-25T10:14:10.644 [Information] 2022-02-25 10:14:10.629 ERROR 2880 --- [pool-2-thread-1]       : Error null
2022-02-25T10:14:10.645 [Information] java.lang.NullPointerException
2022-02-25T10:14:10.645 [Information] at org.springframework.cloud.function.adapter.azure.FunctionInvoker.handleRequest(FunctionInvoker.java:129)
2022-02-25T10:14:10.645 [Information] at ProductController.getProduct(ProductController.java:53)

下面是产品控制器中抛出异常的片段。

代码语言:javascript
复制
@FunctionName("getProduct")
    public HttpResponseMessage getProduct(@HttpTrigger(name = "getRequest", methods = {
            HttpMethod.GET }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<String> getProductRequest,
                                                        ExecutionContext context) {
        ProductRequestDTO productRequestDTO = new ProductRequestDTO();
        try {
             String id = getProductRequest.getQueryParameters().get("id");

            if (id != null) {
                log.info("Request Received for id: " + id);
                productRequestDTO.setId(id);
            }
            return getProductRequest.createResponseBuilder(HttpStatus.OK).body(handleRequest(productRequestDTO,context)).build();
        } 
catch (NullPointerException e) {
            l
log.error("Error " + e.getMessage());
        
            return getProductRequest.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }

    }

pom.xml

代码语言:javascript
复制
=============
<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <azure.functions.maven.plugin.version>1.14.2</azure.functions.maven.plugin.version>
        <azure.functions.java.library.version>1.4.2</azure.functions.java.library.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-adapter-azur
e</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-function-webflux</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
EN

回答 2

Stack Overflow用户

发布于 2022-05-18 08:59:52

我们有一个类似的问题,并通过更新函数扩展版本:~4和其他一些依赖版本解决了这个问题,如下所示

代码语言:javascript
复制
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-dependencies</artifactId>
            <version>3.2.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure.functions</groupId>
            <artifactId>azure-functions-java-library</artifactId>
            <version>${azure.functions.java.library.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-functions-maven-plugin</artifactId>
            <version>${azure.functions.maven.plugin.version}</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.2.0</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.3.0</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>io.gatling</groupId>
            <artifactId>gatling-maven-plugin</artifactId>
            <version>4.1.5</version>
        </plugin>
    </plugins>
</pluginManagement>
票数 0
EN

Stack Overflow用户

发布于 2022-02-28 07:11:07

  • 因为对象不是实例化而是使用的,所以会发生java.lang.NullPointerException
  • 实例化的目的是将实例的地址分配给对象,并且由于获取的对象的实例不存在,所以经常抛出NullPointerException,因此返回的地址为null。如果引用此不存在的实例地址而不首先确定问题情况,则将引发此异常。
  • 您收到了一个NullPointerException,因为创建AzureSpringBootRequestHandler实例的尝试失败了。配置可能有问题。
  • 您输入的null值不应该是造成这种情况的原因。官方文件也以null格式传递。抛出的异常意味着您尚未成功地创建实例。
  • 如果您试图扩展AzureSpringBootRequestHandler并获得一个NullPointException,那么问题很可能是您的AzureSpringBootRequestHandler版本缺少Azure所需的重要功能。

有关更多信息,请参考这些链接。

  1. 蔚蓝中的春云函数
  2. AzureSpringBootRequestHandler.java版本
  3. Azure函数,azureFunctionsPackage任务与java.lang.NullPointerException失败
  4. Java NullPointerException
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71264497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档