Angular15+Api-Platform POST请求报错‘Update is not allowed’求助
问题:Api-Platform POST请求报错
Update is not allowed for this operation的原因分析 问题背景
我在学习Angular 15.0.3开发CRUD功能,应用已成功对接基于Symfony的Api-Platform,GetCollection()、Put()、Get()、Delete()方法均正常工作。但配置Post()方法后,出现报错:Update is not allowed for this operation,直到修改服务代码仅传递name字段后,POST请求才成功。
相关代码
实体代码(PHP)
#[ORM\Entity(repositoryClass: SerieRepository::class)] #[ ApiResource( operations: [ new Meta\GetCollection(), new Meta\Post(), new Meta\Get( uriTemplate: '/series/name/{name}', uriVariables: 'name' ), new Meta\Get(), new Meta\Patch(), new Meta\Delete() ], normalizationContext: [ 'groups' => ['series:read'] ], denormalizationContext: [ 'groups' => ['series:post'] ], ) ] class Serie { #[ ORM\Id, ORM\GeneratedValue, ORM\Column, Meta\ApiProperty(identifier: true), Groups(['series:read','books:read']) ] private ?int $id = null; #[ ORM\Column(length: 100, unique: true), Groups(['series:read','series:post','books:read']) ] private ?string $name = null; #[ ORM\OneToMany(mappedBy: 'serie', targetEntity: Book::class), Groups(['series:read']) ] private Collection $books; public function __construct() { $this->books = new ArrayCollection(); } // ... 其他代码 }
实体服务代码(TypeScript)
最初版本(报错)
create(serie: Serie): Observable<Serie> { return this.http.post<Serie>( 'http://127.0.0.1:8000/api/series', serie, { headers: {'Content-Type':'application/json'}, responseType: "json", } ) }
修改后版本(正常工作)
create(serie: Serie): Observable<Serie> { return this.http.post<Serie>( this.seriesurl, { 'name': serie.name }, { headers: {'Content-Type':'application/json'}, responseType: "json", } ) }
组件代码(TypeScript)
export class SeriesPostComponent { serie: Serie; constructor(private _formBuilder: FormBuilder, private serieService: SerieService, private snackBar: SnackBarComponent) { this.serie = new Serie(0,"",[]); } nameFormGroup = this._formBuilder.group({ firstCtrl: ['', Validators.required], }); addSerie(): void { this.serie.name = this.nameFormGroup.value.firstCtrl ?? ''; this.serieService.create(this.serie as Serie).subscribe( data => { this.snackBar.successSnackBar(`la série < ${data.name} > à été ajouté`) }, echec => { this.snackBar.errorSnackBar(echec.error.detail) console.log(echec); } ); } }
原因分析
- ID字段传递导致操作混淆:初始请求中传递了
id:0和空books数组。Api-Platform的POST集合操作用于创建新资源,主键ID由数据库自动生成,创建时无需传递。当请求体包含ID字段时,Api-Platform会误认为你要更新ID=0的已存在资源,但当前POST操作配置的是集合创建,不允许更新,因此触发报错。 - 序列化分组限制+逻辑冲突:实体的
denormalizationContext指定了series:post分组,仅包含name字段,id和books不在此分组内。Api-Platform会拒绝处理分组外的字段,但更关键的是传递的非空ID(0)打破了创建操作的预期逻辑,导致路由匹配和操作类型判断出错。 - 修改后请求符合创建逻辑:仅传递
name字段时,请求体无ID,完全符合series:post分组要求,Api-Platform明确识别为创建新资源的请求,因此正常执行。
内容的提问来源于stack exchange,提问作者Fafnir
相关产品推荐
相关产品推荐

