【问题标题】:Angular requisition http method GetAngular 申请 http 方法 Get
【发布时间】:2021-11-22 18:37:49
【问题描述】:

我对通过 post 方法的 http 请求有疑问。 然后发送到具有映射为端点的后端

我的汽车控制器

@RequestMapping(value = "/car")
@AllArgsConstructor
@CrossOrigin
public class CarController {

@PostMapping(value = "/test/{code}")
    public ResponseEntity<String> getCars(@PathVariable(required = true) String code) throws URISyntaxException {
        return ResponseEntity.ok("Response OK");
    }
}

我的环境

export const environment = {
  production: false,
  apiEndpoint: "http://localhost:8080/my-app/"
};

我的 app.service.component.ts

httpOptions = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
  };

getCars() {
    const url = `${environment.apiEndpoint}`+ "/car/teste/?" + `${code}`;
    this.http.get<any>(url).subscribe(resp => {
    console.log(resp)
   });
}

错误日志

请求出现 404 错误并显示以下消息

错误{正文:{…},网址:'http://localhost:8080/car/test/',标题: HttpHeaders,状态:404,状态文本:'未找到'}正文:{错误: “找不到集合'汽车'”}标头:HttpHeaders {normalizedNames: 地图(0),lazyUpdate:空,lazyInit:ƒ}状态:404statusText:“不是 找到"url:"http://localhost:8080/my-app/car/test"[[Prototype]]: 对象

【问题讨论】:

    标签: angular spring-boot


    【解决方案1】:

    您将代码参数作为查询参数传递,而不是路由的参数。这就是它找不到汽车的原因。

    【讨论】:

    • 我的端点返回 System.out.println("code") 和 response.OK("Teste") 不要调用存储库...
    【解决方案2】:

    不确定您的意思是发出get 请求,因为上面的方法是get 请求。

    无论如何,请仔细检查您的 URL 拼写,因为在 API 端点中,URL 是 /test/{code},而在客户端中,URL 是 ".../teste/?"...

    teste 中的 e 可能会导致 404

    【讨论】:

    • 我的 url 角度:const url = ${environment.apiEndpoint}+ "/test/" + ${code};
    猜你喜欢
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多