2013-08-26 121 views
1

我正在使用node.js和spring web服務。我在tomcat服務器上部署了spring web服務應用程序,節點正在運行。我正在使用一個Web應用程序,它將調用節點,並從節點調用Spring REST Web服務。我能夠獲得在該節點的JSON數據,但同時從節點呼喚春天REST Web服務我收到以下錯誤:如何通過使用節點傳遞JSON主體來調用REST服務?

at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:202) 
    at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:136) 
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:159) 
    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257) 
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222) 
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211) 
    at org.jboss.resteasy.springmvc.ResteasyHandlerAdapter.createModelAndView(ResteasyHandlerAdapter.java:87) 
    at org.jboss.resteasy.springmvc.ResteasyHandlerAdapter.handle(ResteasyHandlerAdapter.java:74) 
    at org.jboss.resteasy.springmvc.ResteasyHandlerAdapter.handle(ResteasyHandlerAdapter.java:24) 
    at org.jboss.resteasy.springmvc.ResteasyWebHandlerTemplate.handle(ResteasyWebHandlerTemplate.java:39) 
    at org.jboss.resteasy.springmvc.ResteasyHandlerAdapter.handle(ResteasyHandlerAdapter.java:45) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at com.project.webservices.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:76) 
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:722) 
Caused by: java.io.EOFException: No content to map to Object due to end of input 
    at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2775) 
    at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2691) 
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315) 
    at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419) 
    at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:105) 
    at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:63) 
    at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:108) 
    at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:169) 
    ... 37 more 

以下是我的Node.js代碼:

var express = require("express"), 
    app = express(), 
    http = require("http").createServer(app); 
    var requestObj = require('request'); 

    var responseBody = ""; 

    app.use(express.bodyParser()); 

    function getSample(token){ 
     requestObj({ 
       url : "http://127.0.0.1:8080/project/sample/get", 
       method : "POST", 
       headers : { "Content-Type" : "application/json","pubKey":token} 
      }, 
      function (error, response, body) { 
       responseBody=body; 
      } 
     ); 
     console.log("responseBody:= "+responseBody); 
    } 

    function something(token,jsonbody){ 
     console.log("jsonbody :- "+JSON.stringify(jsonbody)); 
     requestObj({ 
       url : "http://127.0.0.1:8080/project/something/childurl", 
       method : "POST", 
       headers : { "Content-Type" : "application/json","pubKey":token}, 
       bosy : JSON.stringify(jsonbody) 
      }, 
      function (error, response, body) { 
       responseBody=body; 
      } 
     ); 
     console.log("responseBody:= "+responseBody); 
    } 

    app.post('/samples', function(request, response){ 
     console.log(request.body); 
     var methodName = request.method; 
     var token = request.body.pubKey; 
     console.log("Called method is :-"+methodName); 
     if(methodName=="POST"){ 
      getSample(token); 
     } 
     response.send(responseBody); 
    }); 


    app.post('/something', function(request, response){ 
     console.log(request.body); 
     var methodName = request.method; 
     var token = request.body.pubKey; 
     console.log("Called method is :-"+methodName); 
     if(methodName=="POST"){ 
      something(token,request.body); 
     } 
     response.send(responseBody); 
    }); 


    app.listen(8088); 
+0

看來你有一個錯字: 「bosy:JSON.stringify(jsonbody)」 - > 「身體:......」 –

+0

我編輯相同,但錯誤依然存在。我仍然無法進入Web服務調用並獲得相同的上述錯誤。 另外還有一件事我只是看着服務器日誌,實際上我傳遞了一個JSON並將內容類型設置爲「application/json」,但是在響應中顯示給我「Content-Type:text/html; charset = utf-8 「(這個響應是通過Firefox的RESTClient添加的),如果我使用RESTClient工具直接調用REST URL而不使用node.js,那麼它會返回我的響應並執行該REST Web服務中的代碼。 – abcd

回答

1

在函數something()中,你缺少body:參數。另外,如果您有json有效負載和響應,則應使用json:true

requestObj({ 
       url : "http://127.0.0.1:8080/project/something/childurl", 
       method : "POST", 
       headers : { "Content-Type" : "application/json","pubKey":token}, 
       body : JSON.stringify(jsonbody), 
       json: true 
      }, 
      function (error, response, body) { 
       responseBody=body; 
      } 
     ); 
+0

感謝您的評論並指出了錯字。但錯誤仍然存​​在。我仍然無法進入Web服務調用並獲得相同的上述錯誤。 – abcd

+0

另外還有一件事我只是看着服務器日誌,實際上我傳遞了一個JSON並將內容類型設置爲「application/json」,但是在響應中顯示給我「Content-Type:text/html; charset = utf-8 「(這個響應是通過Firefox的RESTClient添加的),如果我使用RESTClient工具直接調用REST URL而不使用node.js,那麼它會返回我的響應並執行該REST Web服務中的代碼。 – abcd

+0

你能夠調試java部分嗎?很高興看到,你是否擊中目標控制器/操作或輸入有問題。不幸的是,我不知道tomcat/springmvc,所以日誌不會說太多.. –

相關問題