2015-10-14 90 views
0

我有一個前端web的應用程序,它通過一個簡單的形式上傳文件和其他數據:如何使用apache-camel restlet組件處理文件上傳?

<form action="/document/invoice/entity/777" enctype="multipart/form-data"> 
    <input name="file" type="file"> 
    <input name="userId"> 
    <button>Upload File</button> 
    </form> 

和服務器端必須是與的Restlet組件處理上傳數據的Apache的駱駝路線,類似這樣的內容:

<restContext id="myApi" xmlns="http://camel.apache.org/schema/spring"> 

    <rest path="/document"> 
     <post uri="/{documentType}/entity/{entityId}" outType="documentService.Result" 
      consumes="multipart/form-data" produces="application/json"> 
      <route> 
       <to uri="bean:documentService?method=validate(${body.file}, ${body.userId}, ${header.documentType}, ${header.entityId})"/> 
      </route> 
     </post> 
    </rest> 

</restContext> 

有了這個配置我得到這個錯誤:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 
    at com.google.gson.Gson.fromJson(Gson.java:822) 
    at com.google.gson.Gson.fromJson(Gson.java:748) 
    at org.apache.camel.component.gson.GsonDataFormat.unmarshal(GsonDataFormat.java:105) 
    at org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:67) 
    at org.apache.camel.processor.binding.RestBindingProcessor.process(RestBindingProcessor.java:175) 
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) 
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:448) 
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191) 
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:118) 
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:80) 
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191) 
    at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109) 
    at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:87) 
    at org.apache.camel.component.http.CamelServlet.service(CamelServlet.java:144) 
    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:537) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658) 
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:744) 
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387) 
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:183) 
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145) 
    at com.google.gson.Gson.fromJson(Gson.java:810) 
    ... 36 more 

如何上傳文件和行吟詩人r數據到apache-camel rest端點?

回答

0

看起來你有一些配置問題,你如何將數據傳遞給駱駝的restlet組件。我建議閱讀駱駝網站上的restlet文檔:http://camel.apache.org/restlet.html如果您仍然遇到問題,請鏈接到一個完整的項目,該工程使用駱駝執行restlet服務器: http://www.ofbizian.com/2012/02/apache-camel-rest-example.html

如果檢查出這些資源後仍然有問題,請澄清一下,如果該異常來自客戶端或服務器端。

+0

我已經閱讀了幾次文檔,沒有上傳文件的運氣。創建CRUD端點沒有問題,但上傳文件和其他數據對我來說是一件困難的事情。謝謝。 –

0

您正在嘗試上傳一些數據或文件,但根據錯誤期望JSON數據「com.google.gson.JsonSyntaxException」,而是獲取其他內容。您確定後端部分根據您發送的內容正確配置嗎?

+0

關鍵是如何正確配置其餘端點以處理來自該類型帖子的上傳?謝謝。 –

+0

由於你得到JSON錯誤,你需要查看你的代碼,因爲你有生產=「application/json」。那麼,你真的在​​製作JSON嗎? –