2017-04-06 78 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