gpt4 book ai didi

java - 没有自定义编码(marshal)拆收器或@DynamoDBDocument 注释无法编码(marshal)类型类

转载 作者:行者123 更新时间:2023-11-30 08:50:55 34 4
gpt4 key购买 nike

错误:com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException:无法编码类型类 [Ljava.lang.String;没有自定义编码(marshal)拆收器或@DynamoDBDocument 注释。

我想将数据从 java Spring MVC REST 存储到 DynamoDB。我正在发出 post 请求,它调用一种方法将所有数据加载到 Amazon DynamoDB。这是我的代码。休息 Controller :

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

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

我已将问题数据加载到接口(interface)常量中。 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 来 self 的控制台。

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()。您可以使用 Set 或 List 代替 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

他们从不使用数组。

关于java - 没有自定义编码(marshal)拆收器或@DynamoDBDocument 注释无法编码(marshal)类型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30758794/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com