【问题标题】:Rest API responds with html when deployed to heroku部署到 heroku 时,Rest API 以 html 响应
【发布时间】:2020-02-11 10:41:32
【问题描述】:

我正在尝试部署一个不涉及任何类型的 UI/html 文件的 rest api。我只是希望它用一些 json 响应体来响应,就是这样。我正在使用 spring 和 java 11。这个请求在 localhost 中完美运行,并以 JSON 正文响应。

POST /api/v1/character-info HTTP/1.1
Host: localhost:6969
Content-Type: application/json
Accept: application/json
Cache-Control: no-cache
Postman-Token: 75913809-8b11-494f-b5ac-dfd6c3cecbb1

{
    "accountId": "123123",
    "teamNames": [
        "pepper",
        "heath"
    ]
}

但是,当我将我的应用程序部署到 heroku 时,相同的请求不再有效。我将 localhost:6969 更改为 https://myapp.herokuapp.com,它会回复此 html 正文:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta charset="utf-8">
        <title>Application Error</title>
        <style media="screen">
          html,body,iframe {
            margin: 0;
            padding: 0;
          }
          html,body {
            height: 100%;
            overflow: hidden;
          }
          iframe {
            width: 100%;
            height: 100%;
            border: 0;
          }
        </style>
    </head>
    <body>
        <iframe src="//www.herokucdn.com/error-pages/application-error.html"></iframe>
    </body>
</html>

我如何确保它将返回 json 而不是 HTML?

这是我的控制器:

@ResponseBody
@ResponseStatus(HttpStatus.OK)
@PostMapping("/api/v1/character-info")
public CharacterInfoRequest saveTeamNames(@Valid @RequestBody CharacterInfoRequest request) {
    Optional<TosAccount> characterOptional = tosAccountRepository.findByInGameAccountId(request.getAccountId());
    TosAccount character = characterOptional
            .orElseGet(() -> tosAccountService.createTosAccount(request.getAccountId()));

    characterService.updateTeamNames(character, request.getTeamNames());

    return request;
}

非常感谢您的帮助。

heroku 的日志:

2020-01-11T16:42:40.304269+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=POST path="/api/v1/character-info" host=myapp.herokuapp.com request_id=09a39688-c8a6-4c9a-97cf-75355d2da244 fwd="43.226.7.26" dyno= connect= service= status=503 bytes= protocol=https
    2020-01-11T16:48:20.862616+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=myapp.herokuapp.com request_id=c8f8ad30-437a-4683-93aa-0ab37b403a61 fwd="43.226.7.26" dyno= connect= service= status=503 bytes= protocol=https
    2020-01-11T16:48:21.239211+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=myapp.herokuapp.com request_id=38de869d-5712-4e8c-b263-fc8e8240df2b fwd="43.226.7.26" dyno= connect= service= status=503 bytes= protocol=https
    2020-01-11T16:48:45.871749+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=POST path="/api/v1/character-info" host=myapp.herokuapp.com request_id=befd63a9-3b3b-400f-a559-e4711973960c fwd="43.226.7.26" dyno= connect= service= status=503 bytes= protocol=https

这是我的 Procfile:

worker: java -jar -Dserver.port=$MY_APP_PORT build/libs/*.jar

【问题讨论】:

  • 返回的 HTML 是 Heroku 的应用程序错误。您的应用程序日志说什么?您可以使用heroku logs 命令检查它们。
  • 您好添加了日志。请帮帮我。

标签: java spring heroku


【解决方案1】:

Heroku 的 503 响应 HTML 和“没有运行 Web 进程”错误表明您的 Web 服务器进程没有运行,因此它无法提供 JSON 响应。

您可以在 Heroku 文档中找到有关 Heroku 错误代码的更多信息,例如此错误代码,例如此错误代码为 H14:https://devcenter.heroku.com/articles/error-codes#h14-no-web-dynos-running

他们的文档建议将您的 Web 进程扩展到至少 1 dyno 才能运行。但是,我在您的 Procfile 中注意到您没有列出 Web 进程,您只有工作进程。在 Heroku 上,工作进程未连接到 HTTP 端点。您需要为此定义一个 Web 进程。

尝试将您的个人资料更改为:

web: java -jar -Dserver.port=$PORT build/libs/*.jar

确保提交并部署此更改。

然后,使用 Heroku Web 界面扩展您的 Web 进程,或者如果您使用 CLI 界面,则可以使用 Heroku Toolbelt:

heroku ps:scale web=1

【讨论】:

  • 我试过你写的,但很遗憾没有奏效。 :( 我收到错误代码 H10。
  • 终于成功了。谢谢你的指导。完成您的建议后,我将 procfile 更改为web: java -jar -Dserver.port=$PORT build/libs/*.jar
猜你喜欢
  • 2021-10-15
  • 2018-08-06
  • 2016-03-11
  • 2020-09-05
  • 2017-11-28
  • 2013-06-24
  • 2018-12-08
  • 2020-08-26
  • 2021-05-22
相关资源
最近更新 更多