Error: com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Cannot marshall type class [Ljava.lang.String; without a custom marshaler or @DynamoDBDocument annotation.
I want to store data to DynamoDB from java Spring MVC REST. I am making the post request which calls a method to load all the data to Amazon DynamoDB. Here is my code. RestController:
@RestController
public class QuestionResponseController
{
@RequestMapping(value="/loadQuestionTableData",method=RequestMethod.POST,produces={"application/json"})
public DynamoDBMapper loadQuestionTableData()
{
return LoadQuestionTableData.loadQuestionTableData();
}
}
This is my Question.java (POJO for Question table in DynamoDB): Question.java
import java.util.Arrays;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
@DynamoDBTable(tableName = "Question")
public class Question
{
private int q_id;
private String text;
private String[] options;
private String contraint;
@DynamoDBHashKey(attributeName = "options")
public String[] getOptions() {
return options;
}
public void setOptions(String[] options) {
this.options = options;
}
@DynamoDBHashKey(attributeName = "q_id")
public int getQ_id() {
return q_id;
}
public void setQ_id(int q_id) {
this.q_id = q_id;
}
@DynamoDBAttribute(attributeName = "text")
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@DynamoDBAttribute(attributeName = "contraint")
public String getContraint() {
return contraint;
}
public void setContraint(String contraint) {
this.contraint = contraint;
}
} //along with overridden toString(), equals(), hashcode().
Here is LoadQuestionTableData.java where actual loading of data takes place to dynamoDB. LoadQuestionTableData.java
public class LoadQuestionTableData
{
static DynamoDBMapper mapper = null;
public LoadQuestionTableData() {
//constructor
}
protected static Logger logger = Logger.getLogger("LoadQuestionTableData");
public static DynamoDBMapper loadQuestionTableData()
{
try
{
AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());
DynamoDBMapper mapper = new DynamoDBMapper(client);
Question item = new Question();
item.setQ_id(1);
item.setText(IQuestionAnswerData.amQuestionText1);
item.setOptions(IQuestionAnswerData.amOptionsForQn1);
item.setContraint(IQuestionAnswerData.constraintMorning);
mapper.save(item);
item = new Question();
item.setQ_id(2);
item.setText(IQuestionAnswerData.amQuestionText2);
item.setOptions(IQuestionAnswerData.amOptionsForQn2);
item.setContraint(IQuestionAnswerData.constraintMorning);
mapper.save(item);
}
catch(AmazonServiceException ase)
{
ase.printStackTrace();
ase.getMessage();
}
catch (Exception e)
{
e.printStackTrace();
e.getMessage();
}
return mapper;
}
}
I have loaded Questions data into an interface constants. IQuestionAnswerData.java
public interface IQuestionAnswerData
{
static String constraintMorning= "08:00";
static String amQuestionText1 ="Good";
static String amOption11="okay okay";
static String amOption12 = "Better";
static String amOption13 = "nice";
static String amOption14= "awsome!";
static String [] amOptionsForQn1 = new String[]{amOption11,amOption12,amOption13,amOption14};
static String amQuestionText2 ="How is your health?";
String amOption21="I was just about to!";
String amOption22 = "I'll get to it in a bit";
String amOption23 = "Maybe later";
String amOption24= "I would never!";
static String [] amOptionsForQn2 = new String[]{amOption21,amOption22,amOption23,amOption24};
}
And I am getting error as follows. Stacktrace from my console.
19:03:37.345 [tomcat-http--6] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper com.webapp.controller.QuestionResponseController.loadQuestionTableData()]
19:03:37.345 [tomcat-http--6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'questionResponseController'
19:03:37.399 [tomcat-http--6] DEBUG com.amazonaws.AmazonWebServiceClient - Internal logging succesfully configured to commons logger: true
19:03:37.418 [tomcat-http--6] DEBUG com.amazonaws.metrics.AwsSdkMetrics - Admin mbean registered under com.amazonaws.management:type=AwsSdkMetrics
19:03:37.581 [tomcat-http--6] DEBUG c.a.internal.config.InternalConfig - Configuration override awssdk_config_override.json not found.
//Error Starts from here
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Cannot marshall type class [Ljava.lang.String; without a custom marshaler or @DynamoDBDocument annotation.
at com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas$StandardItemConverter.getObjectToMapMarshaller(ConversionSchemas.java:396)
at com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas$StandardItemConverter.augment(ConversionSchemas.java:325)
at com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas$StandardItemConverter.getMarshaller(ConversionSchemas.java:299)
at com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas$StandardItemConverter.convert(ConversionSchemas.java:265)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.execute(DynamoDBMapper.java:1044)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save(DynamoDBMapper.java:948)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save(DynamoDBMapper.java:740)
at com.webapp.service.LoadQuestionTableData.loadQuestionTableData(LoadQuestionTableData.java:33)
at com.webapp.controller.QuestionResponseController.loadQuestionTableData(QuestionResponseController.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:147)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:277)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
19:03:38.061 [tomcat-http--6] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
19:03:38.061 [tomcat-http--6] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
19:03:48.169 [Finalizer] DEBUG o.a.h.i.c.PoolingClientConnectionManager - Connection manager is shutting down
19:03:48.172 [Finalizer] DEBUG o.a.h.i.c.PoolingClientConnectionManager - Connection manager shut down
19:03:48.180 [java-sdk-http-connection-reaper] DEBUG c.a.http.IdleConnectionReaper - Reaper thread:
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method) ~[na:1.8.0_45]
at com.amazonaws.http.IdleConnectionReaper.run(IdleConnectionReaper.java:112) ~[aws-java-sdk-1.9.40.jar:na]
19:03:48.180 [java-sdk-http-connection-reaper] DEBUG c.a.http.IdleConnectionReaper - Shutting down reaper thread.
Kindly help me resolve this error so that I can successfully load items to DynamoDB. TIA.
The problem might be here
public String[] getOptions()
. Instead ofString[]
can you use a Set or List.Look at their examples:
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaArbitraryDataMappingHLAPI.html
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDeclarativeTagsList.html
http://aws.amazon.com/articles/0802321832592496
They never use arrays.