diff --git a/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php
index 873996b..37d7575 100644
--- a/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php
@@ -13,6 +13,7 @@
 use Drupal\Core\StringTranslation\StringTranslationTrait;
 use Drupal\Core\StringTranslation\TranslationInterface;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
 use Symfony\Component\HttpKernel\KernelEvents;
 
@@ -90,18 +91,25 @@ public function __construct(MaintenanceModeInterface $maintenance_mode, ConfigFa
    *   The event to process.
    */
   public function onKernelRequestMaintenance(GetResponseEvent $event) {
-    $route_match = RouteMatch::createFromRequest($event->getRequest());
+    $request = $event->getRequest();
+    $route_match = RouteMatch::createFromRequest($request);
     if ($this->maintenanceMode->applies($route_match)) {
       // Don't cache maintenance mode pages.
       \Drupal::service('page_cache_kill_switch')->trigger();
+
       if (!$this->maintenanceMode->exempt($this->account)) {
         // Deliver the 503 page if the site is in maintenance mode and the
         // logged in user is not allowed to bypass it.
+
+        // If the request format is not 'html' then show default maintenance
+        // mode page else show a text/plain page with maintenance message.
+        if ($request->getRequestFormat() !== 'html') {
+          $response = new Response($this->getSiteMaintenanceMessage(), 503, array('Content-Type' => 'text/plain'));
+          $event->setResponse($response);
+          return;
+        }
         drupal_maintenance_theme();
-        $content = Xss::filterAdmin(SafeMarkup::format($this->config->get('system.maintenance')->get('message'), array(
-          '@site' => $this->config->get('system.site')->get('name'),
-        )));
-        $response = $this->bareHtmlPageRenderer->renderBarePage(['#markup' => $content], $this->t('Site under maintenance'), 'maintenance_page');
+        $response = $this->bareHtmlPageRenderer->renderBarePage(['#markup' => $this->getSiteMaintenanceMessage()], $this->t('Site under maintenance'), 'maintenance_page');
         $response->setStatusCode(503);
         $event->setResponse($response);
       }
@@ -122,6 +130,18 @@ public function onKernelRequestMaintenance(GetResponseEvent $event) {
   }
 
   /**
+   * Gets the site maintenance message.
+   *
+   * @return \Drupal\Component\Render\MarkupInterface
+   *   The formatted site maintenance message.
+   */
+  protected function getSiteMaintenanceMessage() {
+    return SafeMarkup::format($this->config->get('system.maintenance')->get('message'), array(
+      '@site' => $this->config->get('system.site')->get('name'),
+    ));
+  }
+
+  /**
    * Wraps the drupal_set_message function.
    */
   protected function drupalSetMessage($message = NULL, $type = 'status', $repeat = FALSE) {
diff --git a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php
index bc6f053..2350899 100644
--- a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php
+++ b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php
@@ -184,6 +184,22 @@ public function testSerializerResponses() {
   }
 
   /**
+   * Verifies site maintenance mode functionality.
+   */
+  protected function testSiteMaintenance() {
+    $view = Views::getView('test_serializer_display_field');
+    $view->initDisplay();
+    $this->executeView($view);
+
+    // Set the site to maintenance mode.
+    $this->container->get('state')->set('system.maintenance_mode', TRUE);
+
+    $this->drupalGetWithFormat('test/serialize/entity', 'json');
+    // Verify that the endpoint is unavailable for anonymous users.
+    $this->assertResponse(503);
+  }
+
+  /**
    * Sets up a request on the request stack with a specified format.
    *
    * @param string $format
diff --git a/core/modules/system/src/Tests/System/SiteMaintenanceTest.php b/core/modules/system/src/Tests/System/SiteMaintenanceTest.php
index 6a86083..49aff66 100644
--- a/core/modules/system/src/Tests/System/SiteMaintenanceTest.php
+++ b/core/modules/system/src/Tests/System/SiteMaintenanceTest.php
@@ -36,7 +36,7 @@ protected function setUp() {
   }
 
   /**
-   * Verify site maintenance mode functionality.
+   * Verifies site maintenance mode functionality.
    */
   protected function testSiteMaintenance() {
     $this->drupalGet(Url::fromRoute('user.page'));
@@ -131,4 +131,20 @@ protected function testSiteMaintenance() {
     $this->assertEqual('Site under maintenance', $this->cssSelect('main h1')[0]);
   }
 
+  /**
+   * Tests responses to non-HTML requests when in maintenance mode.
+   */
+  public function testNonHtmlRequest() {
+    $this->drupalLogout();
+    \Drupal::state()->set('system.maintenance_mode', TRUE);
+    $formats = ['json', 'xml', 'non-existing'];
+    foreach ($formats as $format) {
+      $this->pass('Testing format ' . $format);
+      $this->drupalGet('<front>', ['query' => ['_format' => $format]]);
+      $this->assertResponse(503);
+      $this->assertRaw('Drupal is currently under maintenance. We should be back shortly. Thank you for your patience.');
+      $this->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
+    }
+  }
+
 }
