我在AWS中设置了DynamoDB,我有一个JSP项目,它使用映射器映射到java对象。但是,当我尝试从表中加载任何数据或将任何数据保存到它们时,它不起作用。我正在创建我的用户,然后连接到数据库,然后设置区域并使用客户端创建映射器,然后保存用户。
Users newUser = new Users();
newUser.setUsername(request.getParameter("email"));
newUser.setFirstName(request.getParameter("firstname"));
newUser.setLastName(request.getParameter("lastname"));
newUser.setPassword(request.getParameter("pass"));
newUser.setID(UUID.randomUUID().toString());
//newUser.setID("0002");
//TODO Add new user to DB and set ID
BasicAWSCredentials credentials = new BasicAWSCredentials("*********","************");
AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials);
client.setRegion(Region.getRegion(Regions.US_WEST_2));
DynamoDBMapper mapper = new DynamoDBMapper(client);
mapper.save(newUser);
根据我的阅读,这应该可行但是当我运行代码时,用户会被填充,但永远不会保存在表中。这是映射的用户类
@DynamoDBTable(tableName = "Users")
public class Users {
private String Username;
private String Password;
private String FirstName;
private String LastName;
private String ID;
@DynamoDBHashKey(attributeName = "Email")
public String getUsername() { return Username; }
public void setUsername(String newUsername) { Username = newUsername; }
@DynamoDBAttribute(attributeName = "FirstName")
public String getFirstName() { return FirstName; }
public void setFirstName(String newFirstName) { FirstName = newFirstName; }
@DynamoDBAttribute(attributeName = "LastName")
public String getLastName() { return LastName; }
public void setLastName(String newLastName) { LastName = newLastName; }
@DynamoDBAttribute(attributeName = "Password")
public String getPassword() { return Password; }
public void setPassword(String newPassword) { Password = newPassword; }
@DynamoDBAttribute(attributeName = "UserID")
public String getID() { return ID; }
public void setID(String newID) { ID = newID; }
}
我的桌子有
在里面。有谁知道为什么我不能保存或加载任何数据?
答案 0 :(得分:0)
似乎还没有指定DynamoDB端点URL。请在客户端对象中设置端点值。即使我们使用的是本地DynamoDB(例如localhost:8000),也必须指定端点URL。
AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials);
client.setEndpoint("http://localhost:8000");
client.setRegion(Region.getRegion(Regions.US_WEST_2));
另外请注意,您似乎正在使用弃用的实例化DynamoDB客户端对象的方式。以下方式是优选的,尤其是如果您使用的是最新的AWS DynamoDB SDK。
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(
new BasicAWSCredentials("*********", "************")))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(
"http://localhost:8000",
Regions.US_WEST_2.getName()))
.build();