Symfony FOSRest возвращает HTML вместо JSON - PullRequest
0 голосов
/ 29 августа 2018

Здравствуйте, я работаю над миграцией Symfony 2.7 -> symfony 3.4. Мои маршруты API не работали с момента миграции. Когда я проверяю их, они отправляют мне ответ в 200 htlm / текст вместо JSON. У меня есть verfier мои сущности, мои контроллеры, и я пробую другую конфигурацию пакета, но я не получил результата.

Я работаю с jmserializer.

Вот моя конфигурация Bundle.

fos_rest:
  param_fetcher_listener:
    enabled: true
  body_listener: true
  format_listener: true
  view:
    view_response_listener: 'force'
    formats:
      json : true
      xml: true
    templating_formats:
      html: true
    force_redirects:
      html: true
    failed_validation: HTTP_BAD_REQUEST
    default_engine: twig
  routing_loader:
    default_format: json
    prefix_methods: false

1 Ответ

0 голосов
/ 29 августа 2018

Этот работал для меня. Вы можете попробовать это.

fos_rest:
    allowed_methods_listener: true
    access_denied_listener:
        json: true
    body_listener:
        enabled: true
        default_format: json
        decoders:
            json: fos_rest.decoder.json
    body_converter:
        enabled: true
        validate: true
        validation_errors_argument: validationErrors
    exception:
        enabled: false
        exception_controller: 'AppBundle\Controller\DefaultController::showApiException'
        codes:
            'Symfony\Component\HttpKernel\Exception\NotFoundHttpException': 404
            'Symfony\Component\HttpKernel\Exception\BadRequestHttpException': 400
            'Symfony\Component\Security\Core\Exception\AccessDeniedException': 403
            'Symfony\Component\Security\Core\Exception\DisabledException': 403
            'Doctrine\DBAL\Exception\ServerException': 500
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
    format_listener:
        rules:
            - { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: false }
    param_fetcher_listener:
        enabled:  true
        force:    true
    routing_loader:
        default_format: json
    view:
        view_response_listener: 'force'
        formats:
            json: true
...