2013-01-04 33 views
0
In the jsp i am passing the data as json,as in the controller method i am trying to map the data to Personbean object using @RequestMapping. But its throwing an exception. 

@ResponseBody工作正常,如果我刪除@ResponseBody代碼。 請建議我做錯了什麼? 我粘貼了代碼以供參考。在Spring mvc應用程序中,@RequestBody引發java.io.EOFException

//jsp (added the data as json and also content type) 
    <script> 
     function abc(){ 
      alert('here!!'); 



      $.ajax({ 
       url:'anki', 
       type:'GET', 
       data:({name:"me"}), 
       dataType: 'json', 
       contentType: 'application/json',  
       success:function(data){ 
       alert('here gain'); 

       $('#time').html(data.name); 


      } 
      }); 
     } 
     </script> 
    <input type="button" name="submit" title="submit" onclick="abc()"/> 
    <div id="time"></div> 


//PersonBean (Bean class) 

package com.controller; 

public class Personbean { 
    private String name; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 


} 

    // controller method 

//(嘗試同時使用和@ResponseBody @RequestBody在此方法)

@RequestMapping(value="/anki",method=RequestMethod.GET) 
     public @ResponseBody EmployeeBean hhh(@RequestBody Personbean pb){ 
      System.out.println(pb.getName()); 
      System.out.println("new method"); 
      EmployeeBean e=new EmployeeBean(); 
      e.setId("1001"); 
      e.setName("ankita"); 
      return e; 
     } 

//i have added the "jacksonMessageConverter" in the dispatcher servlet 

    //dispatcher servlet 

    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> 
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="messageConverters"> 
     <list> 
     <ref bean="jacksonMessageConverter"/> 
     </list> 
    </property> 



//Exception stacktrace(For reference) 

    java.io.EOFException: No content to map to Object due to end of input 
     at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2444) 
     at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2396) 
     at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1662) 
     at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:124) 
     at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154) 
     at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:633) 
     at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:597) 
     at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346) 
     at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171) 
     at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436) 
     at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424) 
     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) 
     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) 
     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669) 
     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574) 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498) 
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394) 
     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) 
     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
     at java.lang.Thread.run(Unknown Source) 

回答

1

試試這個

 $.ajax({ 
     url:'anki', 
     type:'POST', 
     data:JSON.stringify(eval({"name":"me"})), 
     dataType: 'json', 
     contentType: 'application/json',  
     success:function(data){ 
     alert('here gain'); 

     // $('#time').html(data.name); 


    } 
    }); 

不要忘記改變方法在控制器POST

method=RequestMethod.POST 
+0

它不起作用 – user1945064

+0

data:JSON.stringify(eval({「name」:「me」})), 不適用於我....我 – user1945064

+0

你改變類型爲jsp和RequestMethod POST在控制器 – Kris

相關問題