首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >API-平台,在发布时出错。断言触发器,尽管数据是发送的。

API-平台,在发布时出错。断言触发器,尽管数据是发送的。
EN

Stack Overflow用户
提问于 2020-11-27 08:43:01
回答 1查看 267关注 0票数 0

当我使用API平台(Swagger/Postman)发布新数据时,出现了一个错误。有人能解释吗?我有一个可以正常工作的装置。

代码语言:javascript
复制
curl -X POST "http://localhost:8080/countries" -H  "accept: application/json" -H  "Content-Type: application/json" -d "[{\"name\":\"Aruba2\",\"iso2\":\"ZZ\",\"iso3\":\"ZZZ\",\"region\":\"Americas\",\"subregion\":\"Caribbean\",\"capital\":\"Oranjestad\",\"latitude\":12.5,\"longitude\":-69.96666666,\"isEc\":false,\"isEfta\":false}]"

数据具有良好的结构和有效性。

我收到的错误

代码语言:javascript
复制
{
  "type": "https://tools.ietf.org/html/rfc2616#section-10",
  "title": "An error occurred",
  "detail": "name: The country name should not be blank\nname: The country name should not be null\niso2: The iso-2 cannot be blank\niso3: The iso-3 cannot be blank\nregion: The region cannot be blank\nsubregion: The sub-region cannot be blank\ncapital: The capital cannot be blank\nlatitude: The latitude cannot be blank\nlongitude: The longitude cannot be blank\nisEc: The field cannot be blank\nisEfta: The field cannot be blank",
  "violations": [
    {

这是实体代码。

代码语言:javascript
复制
/**
 *
 * @ApiResource(
 *     iri="https://schema.org/Country",
 *     routePrefix="/",
 *     shortName="Countries",
 *     description="API Access to Country Entity",
 *     normalizationContext={"groups"={"country:read"}},
 *     denormalizationContext={"groups"={"country:write"}},
 *     collectionOperations={"GET","POST",
 *         "GETLIST"={
 *             "method"="GET",
 *             "description"="Retrieves a limited set of properties of Country resources",
 *             "path"="country_list_short",
 *             "normalization_context"={"groups":"country:list:short:read"}
 *         }
 *     }
 * )
 *
 * @ORM\Entity(repositoryClass=CountryRepository::class)
 *
 */
class Country extends AbstractEntity

/**
 *
 * @ORM\Column(
 *     name="name",
 *     type="string",
 *     length=128,
 *     nullable=false,
 *     options={"comment":"Country name (English)"}
 * )
 *
 * @Assert\Unique(message="The name {{ value }} already in the table")
 * @Assert\NotBlank(message="The country name should not be blank")
 * @Assert\NotNull(message="The country name should not be null")
 * @Assert\Length(
 *     min="2",
 *     minMessage="The Country name expected minimum length is {{ limit }}",
 *     max="128",
 *     maxMessage="The Country name expected maximum length is {{ limit }}"
 * )
 *
 * @Groups({"country:read","country:write","country:list:short:read"})
 */
private $name;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-27 09:27:57

您正在发送一个JSON数组,而不是JSON对象。

只需删除前面和附加的方括号:

{\"name\":\"Aruba2\",\"iso2\":\"ZZ\",\"iso3\":\"ZZZ\",\"region\":\"Americas\",\"subregion\":\"Caribbean\",\"capital\":\"Oranjestad\",\"latitude\":12.5,\"longitude\":-69.96666666,\"isEc\":false,\"isEfta\":false}

编辑:

此外,您还使用了@Assert\Unique注释。这是一个错误。这个注释必须是与集合一起使用。给定错误消息和属性类型,您希望使用@Assert\UniqueEntity‘,这是一个检查属性在表中是否唯一的类对比。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65034282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档