Angular 8 + Spring 启动:所需的字符串参数不存在

Angular 8 + Spring Boot: required String parameter is not present

当我想调用我的 GET 端点时,我得到 required String parameter is not present 并且我不知道我的代码有什么问题:

Angular:

  getFrameworkPolls(year: string, companyId: string) {
    const params = new HttpParams();
    params.append('year', year);
    params.append('companyId', companyId);
    return this.http.get<IFrameworkPoll[]>(`${this.url}/poll`, {params});
  }

Spring:

@GetMapping("document/frameworks/poll")
public ResponseEntity<List<FrameworkPoll>> findFrameworksWithPolls(@RequestParam String year, @RequestParam String companyId) {
    return pollFacade.findFrameworksWithPolls(year, companyId);
}

错误:

29 Oct 2020;09:40:17.072 [http-nio-8081-exec-7] ERROR  [c.i.e.e.e.GlobalExceptionHandler:78] In global Exception handler
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'year' is not present
    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:202)
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:113)
    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)

HttpParams 是不可变的。 append 方法不修改现有实例。相反,它们 return 个新实例,应用了更改,因此您需要按如下方式分配:

getFrameworkPolls(year: string, companyId: string) {
    const params = new HttpParams();
    params = params.append('year', year);
    params = params.append('companyId', companyId);
    return this.http.get<IFrameworkPoll[]>(`${this.url}/poll`, {params});
  }