邮递员测试顺利进行,但Ajax出错“缺少所需的请求正文”

时间:2019-01-14 08:43:51

标签: javascript java ajax spring-mvc

我想将用户ID发送给控制器,以便获得实名。 例如:

发送:1 3 5 结果:汤姆·贾斯汀·阿尔伯

@RequestMapping(value="/batch",method=RequestMethod.GET)
public String getMore(@RequestBody String userids) {
    String result="";
    String[] ids=userids.split(" ");
    for (String id : ids) {
    Users users =   usersService.findById(Integer.parseInt(id));
    if(users!=null) {
        result= (result+" " + users.getRealname()).trim();
    }
    }
    return result;
}

ajax代码如下

var urln="http://localhost:8080/users/batch";
        $.ajax({
            type: "GET",
            url: urln,
            data:‘1 3’,
            dataType:'text',
            success: function (data) {
                alert(data);
            },
            error: function (XMLHttpRequest, textStatus,
                             errorThrown) {
                alert(textStatus);
            }
        });

它显示错误如下

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.coolspen.rjb.controller.UsersController.getMore(java.lang.String)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:160)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:130)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:126)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:166)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Required request body is missing: public java.lang.String com.coolspen.rjb.controller.UsersController.getMore(java.lang.String)

但邮递员进展顺利,结果如下:

postman goes well

4 个答案:

答案 0 :(得分:1)

您可以看到此链接QueryParam,用于使用GET发送请求。

http://localhost:8080/users/batch?userids=1 3

答案 1 :(得分:0)

您需要这样设置数据:

data:{userids: "1 3"}

答案 2 :(得分:0)

您需要在ajax代码中添加 contentType ,以告诉服务器您要发送的数据类型。请尝试以下操作:

var urln="http://localhost:8080/users/batch";
    $.ajax({
        type: "GET",
        url: urln,
        data:‘1 3’,
        dataType:'text',
        contentType: 'text/plain',
        success: function (data) {
            alert(data);
        },
        error: function (XMLHttpRequest, textStatus,
                         errorThrown) {
            alert(textStatus);
        }
    });

dataType 指定要从服务器返回的数据类型,而 contentType 指定要使用的数据类型发送到服务器。由于Postman固有地设置了包含此contentType的标头,因此来自postman的测试成功。

答案 3 :(得分:0)

尝试一下;

您的变量

var splitCondition = "1 3";

var urln="http://localhost:8080/users/batch?splitCondition";  // <= i have changed this

您的Ajax;

$.ajax({
        type: "GET",
        url: urln,
        accept : "text/html",         // <= changed these lines
        dataType : "html",            // <= changed these lines
        success: function (data) {

            alert(data);
        },
        error: function (XMLHttpRequest, textStatus,
                         errorThrown) {
            alert(textStatus);
        }
    });

您的控制器(更新后的控制器)

@RequestMapping(value="/batch",method=RequestMethod.GET)
@ResponseStatus(value=HttpStatus.OK)
public @ResponseBody String getMore(@RequestParam(value="splitCondition",required=false,defaultValue="")String userids) {
    String result="";
    String[] ids=userids.split(" ");
    for (String id : ids) {
    Users users =   usersService.findById(Integer.parseInt(id));
    if(users!=null) {
        result= (result+" " + users.getRealname()).trim();
    }
    }
    return result;
}