Issue
This Content is from Stack Overflow. Question asked by Siddharth Bhatia
I am trying to integrate dynamo db to my spring boot application I have been following this [link][1] to implement the dynamo db into my spring boot application ever time I try to put into my table I get this error “Unable to unmarshall exception response with the unmarshallers provided“.I have created my dynamo db table manually by using aws console
I have mentioned my code below
Controller
@RestController
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@PostMapping("/emp")
public String data(Employee employee){
return employeeRepository.updateData(employee);
}
Service
@Service
public class EmployeeRepository {
public String updateData(Employee employee) {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable("employee");
Item item = new Item()
.withPrimaryKey("employeeId", "1")
.withString("firstName", "micky")
.withString("lastName", "mouse")
.withString("emailId", "mic@gmail.com");
table.putItem(item);
return "OK";
}
}
DynamoDB config
@Configuration
public class DynamoDBConfiguration {
@Bean
public DynamoDBMapper dynamoDBMapper(){
return new DynamoDBMapper(buildAmazonDynamoDb());
}
private AmazonDynamoDB buildAmazonDynamoDb() {
return AmazonDynamoDBClientBuilder
.standard()
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration("dyno-config")
)
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(
"accessKey","SecretKey"))).build();
}
}
Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@DynamoDBTable(tableName="employee")
public class Employee {
public String employeeId;
public String firstName;
public String lastName;
public String emailId;
}
The values mentioned in employee entity are of same name as of mu dynamoDB entity.Can anyone help me out what is wrong with my code
[1]: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPIItemCRUD.html
Solution
You are using old V1 code. That is not best practice. Look at this section in the same guide that uses DynamoDbClient.
This link uses the AWS SDK for Java V2 and shows you how to successfully develop a Spring REST API that queries Amazon DynamoDB data.
This Question was asked in StackOverflow by Siddharth Bhatia and Answered by smac2020 It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.