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 は正常に動作します。私が間違っていることを提案してください。参考までにコードを貼り付けておきます。

//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)
4

1 に答える 1

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);


    }
    });

コントローラーで Method を POST に変更することを忘れないでください

method=RequestMethod.POST 
于 2013-01-04T05:57:31.713 に答える