我在Symfony 4中使用Symfony Flex和Composer,并在我的应用程序中安装了API平台的服务器组件。
如何在自定义控制器的Swagger (OpenAPI)中添加端点?
<?php
/**
* Application features
*/
namespace App\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* Checks if the service is alive.
*
* @package Behat\Behat\Context\Context
*/
class HealthcheckController
{
/**
* @Route(
* name="api_healthcheck_entrypoint",
* path="/api/ping",
* methods={"GET"},
* )
*/
public function __invoke()
{
return new JsonResponse(array('ping' => 'pong'));
}
}docker-compose exec php bin/console api:swagger:export
{
"swagger": "2.0",
"basePath": "\/",
"info": {
"title": "FastTony.es",
"version": "0.0.1"
},
"paths": {},
"securityDefinitions": {
"apiKey": {
"type": "apiKey",
"in": "header",
"description": "Value for the Authorization header",
"name": "Authorization"
}
},
"security": [
{
"apiKey": []
}
]
}发布于 2019-02-22 20:01:27
您必须在实体中执行此操作。您必须拥有ApiResource注释,才能在其中添加自定义操作。在这种情况下,您必须将此注释放入:
* "api_healthcheck_entrypoint"={
* "controller"=HealthcheckControllerAction::class,
* "method"="GET",
* "path"="/api/ping
* "defaults"={"_api_receive"=false},",
* "normalization_context"={
* "groups"={"ping"}
* }https://api-platform.com/docs/core/operations/#creating-custom-operations-and-controllers
https://stackoverflow.com/questions/54823044
复制相似问题