错误:com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException:无法编组类型类[Ljava.lang.String;没有自定义封送程序或@DynamoDBDocument批注。
我想从java Spring MVC REST将数据存储到DynamoDB。我正在发出post请求,该请求调用将所有数据加载到Amazon DynamoDB的方法。这是我的代码。 的 RestController:
@RestController
public class QuestionResponseController
{
@RequestMapping(value="/loadQuestionTableData",method=RequestMethod.POST,produces={"application/json"})
public DynamoDBMapper loadQuestionTableData()
{
return LoadQuestionTableData.loadQuestionTableData();
}
}
这是我的Question.java(DynamoDB中问题表的POJO): 的 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().
这是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;
}
}
我已将问题数据加载到接口常量中。的 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。
答案 0 :(得分:2)
问题可能出在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
他们从不使用数组。
答案 1 :(得分:0)
我有一个类似的例外;在我的情况下,它是由我使用scala的import UIKit
class ViewController: UIViewController {
var myPhoton : SparkDevice?
var logInOK = false
var deviceOK = false
override func viewDidLoad() {
super.viewDidLoad()
} // End of viewDidLoad
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var loginLabel: UILabel!
@IBOutlet weak var getDeviceLabel: UILabel!
@IBOutlet weak var getTempLabel: UILabel!
@IBOutlet weak var getLightLabel: UILabel!
@IBOutlet weak var lightStateLabel: UILabel!
@IBAction func login(sender: AnyObject) { // Log into the Spark cloud with email address and password
SparkCloud.sharedInstance().loginWithUser("your_user_email_address", password: "your_password") { (error:NSError!) -> Void in
if let e=error {
self.loginLabel.text = "Login Failed"
}
else {
self.loginLabel.text = "Logged in"
self.logInOK = true
}
}
}
@IBAction func getDevice(sender: AnyObject) { // ------ Attempt connection to the Core device
if logInOK {
SparkCloud.sharedInstance().getDevices { (sparkDevices:[AnyObject]!, error:NSError!) -> Void in
if let e = error {
println("Check your internet connectivity")
}
else {
if let devices = sparkDevices as? [SparkDevice] {
//println(sparkDevices)
for device in devices {
if device.name == "your_core_name" {
self.getDeviceLabel.text = device.name
self.myPhoton = device
self.deviceOK = true
}
}
}
}
}
} else {
getDeviceLabel.text = "Plese log in first"
}
//println(myPhoton)
}
引起的;而不是java的
通过明确指定类型List