我正在尝试通过 IntelliJ 中的 Spring Boot 将 AmazonDynamoDB 表与 Java 连接起来。该表的名称是 PATIENT_DATA,其主键是 User_Id,排序键是 Device_Id。
所以,基本上,我有包
- 配置
- 控制器
- 实体
- 存储库
DynamoDBConfig 类
@Configuration
public class DynamoDBConfig {
@Bean
public DynamoDBMapper dynamoDBMapper()
{
return new DynamoDBMapper(buildAmazonDynamoDB());
}
private AmazonDynamoDB buildAmazonDynamoDB() {
return AmazonDynamoDBClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(
"dynamodb.us-west-1.amazonaws.com",
"us-west-1"
)).withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials(
"...USERDEFINED...",
"...USERDEFINED..."
)
)
).build();
}
}
在实体包中,Patient.class 是
@DynamoDBTable(tableName = "PATIENT_DATA")
public class Patient {
@DynamoDBHashKey(attributeName = "User_Id")
public String User_Id;
@DynamoDBRangeKey(attributeName = "Device_Id")
public String Device_Id;
@DynamoDBAttribute(attributeName = "Patient_Id")
public String Patient_Id;
@DynamoDBAttribute(attributeName = "User_Fullname")
public String User_Fullname;
@DynamoDBAttribute(attributeName = "User_Password")
public String User_Password;
@DynamoDBAttribute(attributeName = "Doctor_Name")
public String Doctor_Name;
@DynamoDBAttribute(attributeName = "Doctor_Id")
public String Doctor_ID;
@DynamoDBAttribute(attributeName = "key3")
public String key3;
public String getUser_Id() {
return User_Id;
}
public void setUser_Id(String user_Id) {
User_Id = user_Id;
}
public String getDevice_Id() {
return Device_Id;
}
public void setDevice_Id(String device_Id) {
Device_Id = device_Id;
}
public String getPatient_Id() {
return Patient_Id;
}
public void setPatient_Id(String patient_Id) {
Patient_Id = patient_Id;
}
public String getUser_Fullname() {
return User_Fullname;
}
public void setUser_Fullname(String user_Fullname) {
User_Fullname = user_Fullname;
}
public String getUser_Password() {
return User_Password;
}
public void setUser_Password(String user_Password) {
User_Password = user_Password;
}
public String getDoctor_Name() {
return Doctor_Name;
}
public void setDoctor_Name(String doctor_Name) {
Doctor_Name = doctor_Name;
}
public String getDoctor_ID() {
return Doctor_ID;
}
public void setDoctor_ID(String doctor_ID) {
Doctor_ID = doctor_ID;
}
public String getKey3() {
return key3;
}
public void setKey3(String key3) {
this.key3 = key3;
}
在 PatientController 类内部
public class PatientController {
@Autowired
private PatientRepository patientRepository;
@PostMapping("/patients")
public Patient saveNewPatient(@RequestBody Patient patient)
{
patientRepository.savePatient(patient);
return patient;
}
@GetMapping("/patients/{User_Id}")
public List<Patient> getAllPatientsById(@PathVariable String User_Id)
{
System.out.println("Getting into the function...");
return patientRepository.getAllPatientById(User_Id);
}
患者资料库类内部
@Repository
public class PatientRepository {
@Autowired
public DynamoDBMapper dynamoDBMapper;
public Patient savePatient(Patient patient) {
dynamoDBMapper.save(patient);
return patient;
}
public List<Patient> getAllPatientById(String User_Id) {
Map<String, AttributeValue> eav = new HashMap<>();
//System.out.println(User_Id);
eav.put(":uid", new AttributeValue().withS("USERNAME#13"));
eav.put(":did",new AttributeValue().withS("DEVICE#1001"));
// I Have hardcoded the above variable.
System.out.println(eav);
DynamoDBQueryExpression<Patient> queryExpression = new DynamoDBQueryExpression<Patient>()
.withKeyConditionExpression("User_Id = :uid").withExpressionAttributeValues(eav);
System.out.println(queryExpression);
List<Patient> result = dynamoDBMapper.query(Patient.class, queryExpression);
return result;
}
每当我使用 POSTMAN 工具发送请求时,
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Patient; no mapping for HASH key
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel.hashKey(DynamoDBMapperTableModel.java:119) ~[aws-java-sdk-dynamodb-1.12.40.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel$Builder.build(DynamoDBMapperTableModel.java:449) ~[aws-java-sdk-dynamodb-1.12.40.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardModelFactories$StandardTableFactory.getTable(StandardModelFactories.java:107) ~[aws-java-sdk-dynamodb-1.12.40.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.getTableModel(DynamoDBMapper.java:409) ~[aws-java-sdk-dynamodb-1.12.40.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save(DynamoDBMapper.java:516) ~[aws-java-sdk-dynamodb-1.12.40.jar:na]
at com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.save(AbstractDynamoDBMapper.java:123) ~[aws-java-sdk-dynamodb-1.12.40.jar:na]
at com.hive.patient_credentials.Repository.PatientRepository.savePatient(PatientRepository.java:28) ~[classes/:na]
at com.hive.patient_credentials.Controller.PatientController.saveNewPatient(PatientController.java:20) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.9.jar:5.3.9]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.9.jar:5.3.9]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.3.9.jar:5.3.9]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.9.jar:5.3.9]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.9.jar:5.3.9]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.9.jar:5.3.9]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1064) ~[spring-webmvc-5.3.9.jar:5.3.9]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.9.jar:5.3.9]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.9.jar:5.3.9]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.9.jar:5.3.9]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) ~[tomcat-embed-core-9.0.50.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.9.jar:5.3.9]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.50.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:228) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.50.jar:9.0.50]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.9.jar:5.3.9]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.9.jar:5.3.9]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.9.jar:5.3.9]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.9.jar:5.3.9]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.9.jar:5.3.9]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.9.jar:5.3.9]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1723) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at java.base/java.lang.Thread.run(Thread.java:830) ~[na:na]
我得到这个错误。我检查了 DynamoDB 表中属性的所有大小写匹配,一切正常。事实上,当我尝试将表名更改为 PATIENT_DATASSS 或某个随机值时,它仍然显示错误为 No Mapping for HASH KEY。
请帮我纠正这个问题,因为我不知道如何继续。谢谢。