AngularRest服务使用hal根链接而不是属性

w1e3prcc  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(187)

我有一个用spring运行的restfullapi和一个angular项目来使用我的api。
我在angular项目上安装并设置了此软件包:
npm install@lagoshny/ngx hal client—保存
将service.create与这样的简单实体一起使用时,效果很好:

import {Resource} from '@lagoshny/ngx-hal-client';

export class IngredientCategory extends Resource {
  id: number;
  name: string;
}

但是,当我尝试保存这样的实体时,post请求主体具有链接而不是实体属性:
实体:

import {QuantityType} from './QuantityType';
import {IngredientCategory} from './IngredientCategory';
import {Resource} from '@lagoshny/ngx-hal-client';

export class Ingredient extends Resource {
  id: number;
  name: string;
  quantityType: QuantityType;
  ingredientCategory: IngredientCategory;
}

我使用的方法:

public saveIngredient(name: string, quantityType: QuantityType, ingredientCategory: IngredientCategory): void {
    this.loading = true;
    const newIngredient = new Ingredient();
    newIngredient.name = name;
    newIngredient.ingredientCategory = ingredientCategory;
    newIngredient.quantityType = quantityType;
    this.ingredientsService.create(newIngredient)
      .subscribe(() => {
        this.loadIngredients();
        this.loading = false;
      });
  }

它生成的请求主体:

{
    "name": "test",
    "ingredientCategory": "http://127.0.0.1:8080/ingredientCategories/10",
    "quantityType": "http://127.0.0.1:8080/quantityTypes/13"
}

我想要的请求主体:

{
    "name": "test",
    "quantityType": {
    "id": 10,
    "name": "GRAMME"
    },
    "ingredientCategory": {
    "id": 13,
    "name": "VIANDES"
    }
}

我从spring boot应用程序中得到的错误:
2021-03-20 14:51:03.483警告68111---[nio-8080-exec-4].w.s.m.s.defaulthandlerexceptionresolver:已解析[org.springframework.http.converter.httpMessageNotradableException:json解析错误:无法构造的示例 tapp.models.entities.IngredientCategory (尽管至少存在一个创建者):没有要从字符串值反序列化的字符串参数构造函数/工厂方法('http://127.0.0.1:8080/ingredientcategories/10’);嵌套异常为com.fasterxml.jackson.databind.exc.missmatchdinputException:无法构造的示例 tapp.models.entities.IngredientCategory (尽管至少存在一个创建者):没有要从字符串值反序列化的字符串参数构造函数/工厂方法('http://127.0.0.1:8080/ingredientcategories/10'),位于[源:(pushbackinputstream);行:1,列:37](通过引用链:tapp.models.entities.component[“InCreditCategory”])]
你们知道怎么解决这个问题吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题