api 平台 - 无法为项目生成 IRI

api platform - Unable to generate an IRI for the item

我从 API 平台开始,我正在使用示例实体 "Foo":

namespace AppBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;


/**
 * Foo
 *
 * @ORM\Table(name="foo")
 * @ApiResource
 * @ORM\Entity
 */
class Foo
{
    /**
     * @var string
     *
     * @ORM\Column(name="bar", type="string", length=255, nullable=false)
     */
    private $bar;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;


}

我在仪表板中成功看到了新实体(见图):

API Platform Dhasboard

当我尝试获取所有 Foo 集合时出现以下错误:

curl -X GET "http://127.0.0.1:8000/foos" -H "accept: application/json"

    {
  "type": "https://tools.ietf.org/html/rfc2616#section-10",
  "title": "An error occurred",
  "detail": "Unable to generate an IRI for the item of type \"AppBundle\Entity\Foo\"",
  "trace": [
    {
      "namespace": "",
      "short_class": "",
      "class": "",
      "type": "",
      "function": "",
      "file": "/home/xxxx/api/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php",
      "line": 107,
      "args": []
    },

我尝试了不同的格式并检查了路由:

 julian@js:~/xxxxx/api$ ./bin/console debug:router
 ------------------------------- -------- -------- ------ ----------------------------------- 
  Name                            Method   Scheme   Host   Path                               
 ------------------------------- -------- -------- ------ ----------------------------------- 
  _wdt                            ANY      ANY      ANY    /_wdt/{token}                      
  _profiler_home                  ANY      ANY      ANY    /_profiler/                        
  _profiler_search                ANY      ANY      ANY    /_profiler/search                  
  _profiler_search_bar            ANY      ANY      ANY    /_profiler/search_bar              
  _profiler_phpinfo               ANY      ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results        ANY      ANY      ANY    /_profiler/{token}/search/results  
  _profiler_open_file             ANY      ANY      ANY    /_profiler/open                    
  _profiler                       ANY      ANY      ANY    /_profiler/{token}                 
  _profiler_router                ANY      ANY      ANY    /_profiler/{token}/router          
  _profiler_exception             ANY      ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css         ANY      ANY      ANY    /_profiler/{token}/exception.css   
  _twig_error_test                ANY      ANY      ANY    /_error/{code}.{_format}           
  api_entrypoint                  ANY      ANY      ANY    /{index}.{_format}                 
  api_doc                         ANY      ANY      ANY    /docs.{_format}                    
  api_jsonld_context              ANY      ANY      ANY    /contexts/{shortName}.{_format}            
  api_foos_get_collection         GET      ANY      ANY    /foos.{_format}                    
  api_foos_post_collection        POST     ANY      ANY    /foos.{_format}                    
  api_foos_get_item               GET      ANY      ANY    /foos/{id}.{_format}               
  api_foos_put_item               PUT      ANY      ANY    /foos/{id}.{_format}               
  api_foos_delete_item            DELETE   ANY      ANY    /foos/{id}.{_format}               

 ------------------------------- -------- -------- ------ ----------------------------------- 

我的服务器可能需要一些特殊的库吗?我做错了什么?

提前致谢!

您的 ID

缺少 getter
public function getId()
{
    return $this->id;
}

对于未来的访问者:

如果禁用 json-ld 格式,您将始终收到此错误。检查你的配置文件 ./config/packages/api_platform.yaml:

formats:
        json:     ['application/json']
        jsonld:   ['application/ld+json']
        jsonapi:  ['application/vnd.api+json']
        html:     ['text/html']
        csv:      ['text/csv']