“errormessage”:“在com.persi.student.app类中找不到名为handlerequest的具有适当方法签名的公共方法”

7y4bm7vi  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(366)

对于lambda dynamodb通过api网关获取和发布请求,我得到以下错误:
“errormessage”:“在com.peri.student.app类上找不到名为handlerequest的具有适当方法签名的公共方法”
这是我的主要代码:

package com.persi.Student;

import javax.naming.Context;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;

public class App {

    //static final Logger log= LogManager.getLogger(App.class);
    public static Object handleRequest(Request request, Context context) throws ResourceNotFoundException {

        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
        DynamoDBMapper mapper = new DynamoDBMapper(client);

        Student student = null;
        switch(request.getHttpMethod()) {
        case "GET":
            student = mapper.load(Student.class, request.getId());
            if(student == null) {
                throw new ResourceNotFoundException("Resoure not found:" + request.getId());
            }
            return student;
        case "POST":
            student = request.getStudent();
            mapper.save(student);
            return student;
        default:
            break;
        }       
        return null;
    }

}

这是我的pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.persi</groupId>
  <artifactId>Student</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Student</name>
  <url>http://example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <aws.java.sdk.version>2.13.7</aws.java.sdk.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>17.0</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <version>1.3</version>
      <scope>test</scope>
    </dependency>

    <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.2.0</version>
        </dependency>

    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-lambda -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-lambda</artifactId>
    <version>1.11.918</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-dynamodb -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-dynamodb</artifactId>
    <version>1.11.918</version>
</dependency>

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.14.0</version>
</dependency>

 <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.26</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.0-rc1</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.21</version>
</dependency>  

 <dependency>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>3.2.4</version>
  <type>maven-plugin</type>
</dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

我还将运行时设置正确更新为com.peri.student.app::handlerequest请帮助我。

fcipmucu

fcipmucu1#

你有两个问题需要解决。首先,您的类应该实现以下之一:
com.amazonaws.services.lambda.runtime.requesthandler
com.amazonaws.services.lambda.runtime.requeststreamhandler
如处理程序接口中所述。在您的情况下,您的代码看起来像:

public class App implements RequestHandler<Request, Student>

其次,也许更重要的是处理程序方法 static . lambda运行时找不到您的方法。只需将处理程序更改为t0:

public Student handleRequest(Request request, Context context)

注意,它返回一个 Student 不仅仅是一个物体。这将有助于lambda运行时在返回时序列化正确的对象。

相关问题