2017-04-06 35 views
0

我想图像保存到数据库中,但我得到的错误:杰克逊BLOB例外

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: [email protected]; 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。我必须通过这里的附加信息是什么?

回答

2

我不确定您想要上传文件的方法,但我确定这不是使用Spark时的“推荐”方法。 根据文档,您应该将该文件读取为流,并使用request.raw()来代替request.body()

阅读有关建议的方式here,也有一个github example

+0

我注意到在Post类中是否有一个'java.sql.Blob'类型的东西是个好主意 – Yevgeniy