【问题标题】:How to solve org.springframework.web.bind.MissingServletRequestParameterException error in spring boot and angular?如何解决spring boot和angular中的org.springframework.web.bind.MissingServletRequestParameterException错误?
【发布时间】:2021-10-12 06:26:20
【问题描述】:

我正在尝试将 Angular 9 应用程序的发布请求发送到 Spring Boot,这是发布请求:

public createSuccessMessage(userId: number,tripName: string): Observable<any> {      
    return this.http.post(`${AppComponent.API}/Success/${userId}`, 
    {      
      headers: new HttpHeaders({ 'Accept': 'text/plain', 'Content-Type': 'text/plain' }),
      params: {        
        tripName: tripName
      },
      observe: 'response'
    });
  }

tripName 请求参数由于某种原因未发送。这是控制器的代码。

@PostMapping("/Success/{userId}")   
    public void createSuccessMessage(@PathVariable("userId") String userId, @RequestParam("tripName") String tripName) {    
        
        messagesService.createSuccessMessage(Long.valueOf(userId), tripName);       
    }

我在服务器端收到警告:

Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'tripName' for method parameter type String is not present]

我尝试发送 json,结果相同。

【问题讨论】:

    标签: angular spring-boot post


    【解决方案1】:

    @RequestParam 只对 Get 请求起作用,在控制器中改为 @GetRequest 即可解决问题。

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 2019-07-30
      • 2021-12-07
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      • 2020-07-30
      • 2023-01-23
      • 2019-07-26
      相关资源
      最近更新 更多