Angular HttpClient 获取方法 returns 具有错误命名属性的对象

Angular HttpClient get method returns object with wrongly named attributes

我有一个带有 GET 方法的 Web 控制器 Products。此方法 returns Enumerable 类型 Product.

        public class Product
        {
            public float Price;
            public string Name;
            public string ImageURL;
            public int Quantity;

            public Product()
            {
                this.Quantity = 1;
            }
        }

我创建了一个 Angular 组件,它读取控制器方法提供的数据,如下所示:

constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
    this.grandTotal = 0;
    this.httpClient = http;
    this.url = baseUrl;
    http.get<Product[]>(baseUrl + 'api/SampleData/Products').subscribe(result => {
      this.products = result;
      this.slicedProducts = new Array<Product[]>();
      for (let i = 0; i < this.products.length / 3; i++) {
        this.slicedProducts[i] = this.products.slice(i * 3, i * 3 + 3);
      }

    }, error => console.error(error));
  }

我的问题是 Angular 端的对象中的 属性 名称命名不正确,例如:Product.QuantityProduct.quantity,所以当我尝试使用这些属性稍后在我的函数中,我有一个错误 Quantity 不存在。当我使用 quantity 时,代码有效。

如果您正在使用 Java。检查从 Java 对象到 Json 对象的对象转换设置。假设您在后端使用 Spring 启动,您可能需要默认配置 Jackson to keep the field names as is. I assume the fields are converted to Camel case