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

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

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

  1. package com.persi.Student;
  2. import javax.naming.Context;
  3. import org.apache.log4j.LogManager;
  4. import org.apache.log4j.Logger;
  5. import org.apache.log4j.spi.LoggerFactory;
  6. import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
  7. import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
  8. import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
  9. public class App {
  10. //static final Logger log= LogManager.getLogger(App.class);
  11. public static Object handleRequest(Request request, Context context) throws ResourceNotFoundException {
  12. AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
  13. DynamoDBMapper mapper = new DynamoDBMapper(client);
  14. Student student = null;
  15. switch(request.getHttpMethod()) {
  16. case "GET":
  17. student = mapper.load(Student.class, request.getId());
  18. if(student == null) {
  19. throw new ResourceNotFoundException("Resoure not found:" + request.getId());
  20. }
  21. return student;
  22. case "POST":
  23. student = request.getStudent();
  24. mapper.save(student);
  25. return student;
  26. default:
  27. break;
  28. }
  29. return null;
  30. }
  31. }

这是我的pom.xml文件:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.persi</groupId>
  6. <artifactId>Student</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>Student</name>
  10. <url>http://example.com</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <java.version>1.8</java.version>
  14. <aws.java.sdk.version>2.13.7</aws.java.sdk.version>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>com.google.guava</groupId>
  19. <artifactId>guava</artifactId>
  20. <version>17.0</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>junit</groupId>
  24. <artifactId>junit</artifactId>
  25. <version>4.11</version>
  26. <scope>test</scope>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.hamcrest</groupId>
  30. <artifactId>hamcrest-core</artifactId>
  31. <version>1.3</version>
  32. <scope>test</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>com.amazonaws</groupId>
  36. <artifactId>aws-lambda-java-core</artifactId>
  37. <version>1.2.0</version>
  38. </dependency>
  39. <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-lambda -->
  40. <dependency>
  41. <groupId>com.amazonaws</groupId>
  42. <artifactId>aws-java-sdk-lambda</artifactId>
  43. <version>1.11.918</version>
  44. </dependency>
  45. <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-dynamodb -->
  46. <dependency>
  47. <groupId>com.amazonaws</groupId>
  48. <artifactId>aws-java-sdk-dynamodb</artifactId>
  49. <version>1.11.918</version>
  50. </dependency>
  51. <!-- https://mvnrepository.com/artifact/log4j/log4j -->
  52. <dependency>
  53. <groupId>log4j</groupId>
  54. <artifactId>log4j</artifactId>
  55. <version>1.2.17</version>
  56. </dependency>
  57. <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
  58. <dependency>
  59. <groupId>org.apache.logging.log4j</groupId>
  60. <artifactId>log4j-core</artifactId>
  61. <version>2.14.0</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.slf4j</groupId>
  65. <artifactId>slf4j-log4j12</artifactId>
  66. <version>1.7.26</version>
  67. </dependency>
  68. <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
  69. <dependency>
  70. <groupId>org.slf4j</groupId>
  71. <artifactId>slf4j-simple</artifactId>
  72. <version>1.0-rc1</version>
  73. <scope>test</scope>
  74. </dependency>
  75. <dependency>
  76. <groupId>org.slf4j</groupId>
  77. <artifactId>slf4j-api</artifactId>
  78. <version>1.7.21</version>
  79. </dependency>
  80. <dependency>
  81. <groupId>org.apache.maven.plugins</groupId>
  82. <artifactId>maven-shade-plugin</artifactId>
  83. <version>3.2.4</version>
  84. <type>maven-plugin</type>
  85. </dependency>
  86. </dependencies>
  87. <build>
  88. <plugins>
  89. <plugin>
  90. <groupId>org.apache.maven.plugins</groupId>
  91. <artifactId>maven-compiler-plugin</artifactId>
  92. <version>3.8.1</version>
  93. <configuration>
  94. <source>1.8</source>
  95. <target>1.8</target>
  96. </configuration>
  97. </plugin>
  98. </plugins>
  99. </build>
  100. </project>

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

fcipmucu

fcipmucu1#

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

  1. public class App implements RequestHandler<Request, Student>

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

  1. public Student handleRequest(Request request, Context context)

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

相关问题