 .../Compiler/RemoveJsonapiFormatCompilerPass.php   |  51 ++++++++++
 src/JsonapiServiceProvider.php                     |  14 ++-
 .../Functional/RestJsonApiFormatUnsupported.php    | 100 ++++++++++++++++++++
 tests/src/Functional/RestJsonApiUnsupported.php    | 105 +++++++++++++++++++++
 4 files changed, 268 insertions(+), 2 deletions(-)

diff --git a/src/DependencyInjection/Compiler/RemoveJsonapiFormatCompilerPass.php b/src/DependencyInjection/Compiler/RemoveJsonapiFormatCompilerPass.php
new file mode 100644
index 0000000..50add4c
--- /dev/null
+++ b/src/DependencyInjection/Compiler/RemoveJsonapiFormatCompilerPass.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace Drupal\jsonapi\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+/**
+ * Removes 'api_json' format from the 'serializer.formats' container parameter.
+ *
+ * We want the 'api_json' format to not be supported in the REST module. But the
+ * JSON API module also should not have to define al alternative 'serializer'
+ * service.
+ * This is achieved through removing the 'api_json' format from the
+ * 'serializer.formats' container parameter. The consequences of doing that:
+ *
+ * - the REST module no longer allows this format to be used
+ * - the 'serialization.exception.default' service does not support 'api_json',
+ *   hence a custom exception subscriber is needed, which this module has:
+ *   'jsonapi.exception_subscriber'
+ * - the 'serializer' service does support 'api_json'
+ *
+ * In other words: the 'serializer' service supports 'api_json', but nothing is
+ * aware of it. You could only know by calling 'serializer:supportsEncoding()'.
+ *
+ * @see \Drupal\serialization\RegisterSerializationClassesCompilerPass
+ * @see \Drupal\jsonapi\JsonapiServiceProvider::register()
+ * @see \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber
+ * @see \Drupal\Tests\jsonapi\Functional\RestJsonApiUnsupported
+ */
+class RemoveJsonapiFormatCompilerPass implements CompilerPassInterface {
+
+  /**
+   * Updates the 'serializer.formats' container p
+   *
+   * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
+   *   The container to process.
+   */
+  public function process(ContainerBuilder $container) {
+    if ($container->hasParameter('serializer.formats')) {
+      $filtered_formats = array_filter(
+        $container->getParameter('serializer.formats'),
+        function ($format) {
+          return $format !== 'api_json';
+        }
+      );
+      $container->setParameter('serializer.formats', array_values($filtered_formats));
+    }
+  }
+
+}
diff --git a/src/JsonapiServiceProvider.php b/src/JsonapiServiceProvider.php
index b5f9ea9..f801059 100644
--- a/src/JsonapiServiceProvider.php
+++ b/src/JsonapiServiceProvider.php
@@ -4,13 +4,16 @@ namespace Drupal\jsonapi;
 
 use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Drupal\Core\DependencyInjection\ServiceModifierInterface;
+use Drupal\Core\DependencyInjection\ServiceProviderInterface;
+use Drupal\jsonapi\DependencyInjection\Compiler\RemoveJsonapiFormatCompilerPass;
+use Symfony\Component\DependencyInjection\Compiler\PassConfig;
 
 /**
- * Adds api_json as known format.
+ * Adds 'api_json' as known format and prevents its use in the REST module.
  *
  * @internal
  */
-class JsonapiServiceProvider implements ServiceModifierInterface {
+class JsonapiServiceProvider implements ServiceModifierInterface, ServiceProviderInterface {
 
   /**
    * {@inheritdoc}
@@ -28,4 +31,11 @@ class JsonapiServiceProvider implements ServiceModifierInterface {
     }
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function register(ContainerBuilder $container) {
+    $container->addCompilerPass(new RemoveJsonapiFormatCompilerPass(), PassConfig::TYPE_REMOVE);
+  }
+
 }
diff --git a/tests/src/Functional/RestJsonApiFormatUnsupported.php b/tests/src/Functional/RestJsonApiFormatUnsupported.php
new file mode 100644
index 0000000..1c7d783
--- /dev/null
+++ b/tests/src/Functional/RestJsonApiFormatUnsupported.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace Drupal\Tests\jsonapi\Functional;
+
+use Drupal\Component\Serialization\Json;
+use Drupal\Core\Url;
+use Drupal\node\Entity\Node;
+use Drupal\node\Entity\NodeType;
+use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
+use Drupal\Tests\rest\Functional\ResourceTestBase;
+
+/**
+ * @group jsonapi
+ */
+class RestJsonApiFormatUnsupported extends ResourceTestBase {
+
+  use AnonResourceTestTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  public static $modules = ['jsonapi', 'node'];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $format = 'api_json';
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $mimeType = 'application/vnd.api+json';
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUpAuthorization($method) {
+    $this->grantPermissionsToTestedRole(['access content']);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setUp() {
+    parent::setUp();
+
+    // Set up a HTTP client that accepts relative URLs.
+    $this->httpClient = $this->container->get('http_client_factory')
+      ->fromOptions(['base_uri' => $this->baseUrl]);
+
+    // Create a "Camelids" node type.
+    NodeType::create([
+      'name' => 'Camelids',
+      'type' => 'camelids',
+    ])->save();
+
+    // Create a "Llama" node.
+    $node = Node::create(['type' => 'camelids']);
+    $node->setTitle('Llama')
+      ->setOwnerId(0)
+      ->setPublished(TRUE)
+      ->save();
+  }
+
+  /**
+   * Tests that JSON API is not supported
+   */
+  public function testJsonApiFormatNotSupportedInRest() {
+    // First, verify that 'api_json' does not appear in the 'serializer.foramts'
+    // container parameter, which is what the REST module uses to determine
+    // which formats it supports.
+    $this->assertSame(['json', 'xml'], $this->container->getParameter('serializer.formats'));
+
+    // Second, verify that provisioning a REST resource that lists 'api_json' as
+    // one of its formats
+    $this->provisionResource('entity.node', ['api_json'], []);
+    $this->setUpAuthorization('GET');
+    $url = Node::load(1)->toUrl()->setOption('query', ['_format' => 'api_json']);
+    $response = $this->request('GET', $url, []);
+    $expected_body = Json::encode([
+      'errors' => [
+        [
+          'title' => 'Not Acceptable',
+          'status' => 406,
+          'detail' => 'Not acceptable format: api_json',
+          'links' => [
+            'info' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7',
+          ],
+          'code' => 0,
+        ],
+      ],
+    ]);
+    $this->assertResourceResponse(406, $expected_body, $response);
+  }
+
+  protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {}
+  protected function getExpectedUnauthorizedAccessMessage($method) {}
+  protected function getExpectedBcUnauthorizedAccessMessage($method) {}
+
+}
diff --git a/tests/src/Functional/RestJsonApiUnsupported.php b/tests/src/Functional/RestJsonApiUnsupported.php
new file mode 100644
index 0000000..18e0878
--- /dev/null
+++ b/tests/src/Functional/RestJsonApiUnsupported.php
@@ -0,0 +1,105 @@
+<?php
+
+namespace Drupal\Tests\jsonapi\Functional;
+
+use Drupal\Component\Serialization\Json;
+use Drupal\Core\Url;
+use Drupal\node\Entity\Node;
+use Drupal\node\Entity\NodeType;
+use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
+use Drupal\Tests\rest\Functional\ResourceTestBase;
+
+/**
+ * @group jsonapi
+ */
+class RestJsonApiUnsupported extends ResourceTestBase {
+
+  use AnonResourceTestTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  public static $modules = ['jsonapi', 'node'];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $format = 'api_json';
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $mimeType = 'application/vnd.api+json';
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUpAuthorization($method) {
+    switch ($method) {
+      case 'GET':
+        $this->grantPermissionsToTestedRole(['access content']);
+        break;
+      default:
+        throw new \UnexpectedValueException();
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setUp() {
+    parent::setUp();
+
+    // Set up a HTTP client that accepts relative URLs.
+    $this->httpClient = $this->container->get('http_client_factory')
+      ->fromOptions(['base_uri' => $this->baseUrl]);
+
+    // Create a "Camelids" node type.
+    NodeType::create([
+      'name' => 'Camelids',
+      'type' => 'camelids',
+    ])->save();
+
+    // Create a "Llama" node.
+    $node = Node::create(['type' => 'camelids']);
+    $node->setTitle('Llama')
+      ->setOwnerId(0)
+      ->setPublished(TRUE)
+      ->save();
+  }
+
+  /**
+   * Deploying a REST resource using api_json format results in 406 responses.
+   */
+  public function testApiJsonNotSupportedInRest() {
+    $this->assertSame(['json', 'xml'], $this->container->getParameter('serializer.formats'));
+
+    $this->provisionResource('entity.node', ['api_json'], []);
+    $this->setUpAuthorization('GET');
+
+    $url = Node::load(1)->toUrl()
+      ->setOption('query', ['_format' => 'api_json']);
+    $request_options = [];
+
+    $response = $this->request('GET', $url, $request_options);
+    $expected_body = Json::encode([
+      'errors' => [
+        [
+          'title' => 'Not Acceptable',
+          'status' => 406,
+          'detail' => 'Not acceptable format: api_json',
+          'links' => [
+            'info' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7',
+          ],
+          'code' => 0,
+        ],
+      ],
+    ]);
+    $this->assertResourceResponse(406, $expected_body, $response);
+  }
+
+  protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {}
+  protected function getExpectedUnauthorizedAccessMessage($method) {}
+  protected function getExpectedBcUnauthorizedAccessMessage($method) {}
+
+}
