我正在尝试将图像保存到数据库,但是我收到错误:
org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.sql.Blob, problem: abstract types can only be instantiated with additional type information
at [Source: java.io.StringReader@e95dbd; line: 1, column: 27] (through reference chain: com.soul.seeker.models.Post["postImage"])
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.instantiationException(StdDeserializationContext.java:233)
at org.codehaus.jackson.map.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:60)
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)
at com.soul.seeker.serviceImpl.PostServiceImpl.createPostImage(PostServiceImpl.java:121)
at com.soul.seeker.Application.lambda$main$8(Application.java:142)
at spark.ResponseTransformerRouteImpl$1.handle(ResponseTransformerRouteImpl.java:47)
at spark.http.matching.Routes.execute(Routes.java:61)
at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:130)
at spark.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:50)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
at org.eclipse.jetty.server.Server.handle(Server.java:517)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
at java.lang.Thread.run(Thread.java:745)
Pojo课程:
public class Post{
private String post_id;
private String title;
private String details;
private String username;
private String userImage;
private String url;
private List categories;
private String created_at;
private Blob postImage; //BLOB TYPE
private String postImageName;
//getters setters
}
实施班:
@Override
public void createPostImage(Request request) {
try {
ObjectMapper mapper = new ObjectMapper();
Post createPostImage = mapper.readValue(request.body(), Post.class);
System.out.println("CREATE POST IMAGE" + createPostImage);
} catch (IOException ex) {
Logger.getLogger(PostServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
在线查看类似帖子但找不到任何线索。我该如何解决?它说abstract types can only be instantiated with additional type information
。我必须在这里传递哪些额外信息?
答案 0 :(得分:2)
我不确定您尝试上传文件的方法,但我确定不是"推荐的#34;"使用Spark时的方法。
根据文档,您应该将文件作为流阅读,并使用request.raw()
来实现此目的,而不是request.body()
。
了解推荐的here方式,并且还有github example。