尝试将数据从js发布到spring boot controller

时间:2018-03-14 15:19:09

标签: javascript java ajax spring-boot post

我试图从这个js发布我的数据

$.ajax({
        type: 'POST',
        url: '/url',
        data: {
            arr: tdValues
        },
        success: function () {
            location.reload();
        }
    });

控制器中的所有代码都能正常工作,而来自js的数据不是空的

 @PostMapping("/url")
 public ModelAndView deleteQuestions(@RequestParam(value = "arr[]") String[] tdValues) {
 ModelAndView modelAndView = new ModelAndView();
 modelAndView.setViewName("page");
 return modelAndView;
}

但在那之后我得到了像

这样的例外
Required String[] parameter 'arr[]' is not present

有任何建议如何解决吗?

Stacktrace:

  

必需的String []参数' arr'不存在; org.springframework.web.bind.MissingServletRequestParameterException:必需的String []参数' arr'在org.springframework的org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:198)org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:109)中不存在。 web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument在org.springframework.web.method.support.InvocableHandlerMethod(HandlerMethodArgumentResolverComposite.java:121)在org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)。 invokeForRequest(InvocableHandlerMethod.java:128)位于org.springframework.web.servlet.mvc.method.annotationMappingHandlerAdapter的org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)。 org.springframe上的invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) org.springframework上的org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)中的work.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)。 web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java: 970)org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)位于org.springframework.web.servlet.FrameworkServlet.service的javax.servlet.http.HttpServlet.service(HttpServlet.java:661) (FrameworkServlet.java:846)在javax.servlet.http.HttpServlet.service(HttpServlet.java:742)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)在org.apache.catalina。 core.ApplicationFilterChain.doFilter(ApplicationFilterCh ain.java:166)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)org.apache上的org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) org.springframework.security.web.access.intercept.FilterSecurityInterceptor上的org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:317)中的.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)位于org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java)的org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)中的.invoke(FilterSecurityInterceptor.java:127) :331)在org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)在org.springframework。 security.w eb.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter( Ang.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)的org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:158)中的AnonymousAuthenticationFilter.java:111) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)atg.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)org.springframework.security.web在org.springframework.security.web.savedrequest.RequestCacheAwareFilte的.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)位于org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)的r.doFilter(RequestCacheAwareFilter.java:63): 200)在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)在org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)在org.springframework org.springframework.web.filter.OncePerRequestFilter.doFilter上的org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)中的.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331) (OncePerRequestFilter.java:107)位于org.springframework.security.web.context.SecurityContextPersistenceF的org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331) ilter.doFilter(SecurityContextPersistenceFilter.java:105)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal( webAsyncManagerIntegrationFilter.java:56)org.springframework上的org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)位于org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)的.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java) :347)org.apringframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java) :193)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)在org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)在org.springframework.web.filter。 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)上的OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)at org .springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)org.apringframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain) .java:193)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)at o rg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)org.apache.catalina.core.ApplicationFilterChain.doFilter( ApplicationFilterChain.java:166)在org.apache.catalina的org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)的org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) .core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) )org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)位于org.apache.catalina.core.StandardHostValve的org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)。调用(S tandardHostValve.java:140)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)org.apache.catalina .connector.CoyoteAdapter.service(CoyoteAdapter.java:342)位于org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)的org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:868)org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1459)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)

3 个答案:

答案 0 :(得分:0)

将所需属性(设置为false)添加到@RequestParam。

public ModelAndView deleteQuestions(@RequestParam(required=false, value = "arr[]") String[] tdValues) {

或尝试

public ModelAndView deleteQuestions(@RequestParam(value = "arr") String[] tdValues) {

答案 1 :(得分:0)

一些变化。

  1. 在您的Js代码中。

    $.ajax({
        type: 'POST',
        url: '/url/?arr='+tdValues
        success: function () {
            location.reload();
        }
    });  
    
  2. 在您的控制器代码中,执行此操作。

    public ModelAndView deleteQuestions(@RequestParam(value = "arr") String[] tdValues) {
    

答案 2 :(得分:0)

我的问题以这种方式解决了:

public @ResponseBody ModelAndView deleteQuestions(@RequestParam("arr") Optional<String[]> tdValues) {}

和这个JS:

$.ajax({
        type: 'POST',
        url: '/url?arr='+tdValues,
        error : function() {
            console.log("error");
        },
        success: function () {
            location.reload()
        }
    });