如果没有自定义封送处理程序或@DynamoDBDocument批注,则无法封送类型类



错误:com.amazonaws.services.dynolderbv2.datamodeling.DynamoDBMappingException:如果没有自定义封送处理程序或@DynamoDBDocument注释,则无法封送类型类[Ljava.lang.String;。

我想从JavaSpringMVCREST将数据存储到DynamoDB。我正在进行post请求,该请求调用一个方法将所有数据加载到AmazonDynamoDB中。这是我的密码。RestController:

@RestController
public class QuestionResponseController
{
 @RequestMapping(value="/loadQuestionTableData",method=RequestMethod.POST,produces={"application/json"})
public DynamoDBMapper loadQuestionTableData()
  {
    return LoadQuestionTableData.loadQuestionTableData();
  }
}

这是我的Question.java(DynamoDB中Question表的POJO(:问题.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().

这里是LoadQuestionTableData.java,数据的实际加载发生在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;
  }
} 

我已将Questions数据加载到接口常量中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};
}

我的错误如下Stacktrace

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.

请帮我解决这个错误,这样我就可以成功地将项目加载到DynamoDB。TIA。

问题可能在这里public String[] getOptions()。您可以使用集合或列表来代替String[]

看看他们的例子:
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

他们从不使用数组。

我有一个类似的异常;在我的情况下,它是由我使用scala的List引起的;而不是java的

通过显式指定类型util.List[String]

修复了它

相关内容

  • 没有找到相关文章

最新更新