JSON Patch (2)

启动时,定位到docker-compose.yml所在文件夹,运行docker-compose up,然后在浏览器访问localhost:8080/swagger,应用在启动后会自动创建ExampleDb数据库并插入一条数据。笔者也写了一个获取信息的接口/api/Persons,返回值如下:

[ { "name": "LeBron James", "oId": "5af995a5b8ea8500018d54b7" } ]

然后再使用返回的oId请求/api/Persons/{id}(UpdateThenAddThenRemoveAsync)接口,body的 JsonPatch 描述则用:

/* body */ [ { "value": "Daby", "path": "FirstName", "op": "replace" }, { "value": "Example Address", "path": "Address", "op": "add" }, { "path": "Mail", "op": "remove" } ] /* PersonsController.cs */ [HttpPatch("{id}")] public async Task<PersonDto> UpdateThenAddThenRemoveAsync(string id, [FromBody] JsonPatchDocument<Person> personPatch) { var objectId = new ObjectId(id); var person = await _personRepository.GetAsync(objectId); personPatch.ApplyTo(person); await _personRepository.UpdateAsync(person); return new PersonDto { OId = person.Id.ToString(), Name = $"{person.FirstName} {person.LastName}" }; }

其他相关代码另请查阅。不过需要再提一点的是,Visual Studio 15.7 版本对docker-compose.yml的文本语法解析有些问题,详见MSBuild failing to parse a valid compose file,比如以下代码将无法编译:

environment: - ASPNETCORE_ENVIRONMENT=Development - ASPNETCORE_URLS=http://0.0.0.0:80 - ConnectionString=${MONGODB:-mongodb://mongodb} - Database=ExampleDb 参考文献

JSON Patch

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wssfpd.html