java - Rest API 在部署到 heroku 时以 html 响应

标签 java spring heroku

我正在尝试部署一个rest api,而不涉及任何类型的UI/html 文件。我只是想让它响应一些 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?

这是我的 Controller :

@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

最佳答案

来自 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

关于java - Rest API 在部署到 heroku 时以 html 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59696536/

相关文章:

java - MongoDB 驱动程序作为 Jboss 中连接池的核心模块

java - 未选择 Spring Boot 测试配置

java - Spring mvc 分页

node.js - Node-Foreman 在部署到 Heroku 时不加载环境变量

通过 Heroku 部署时 CSS 在 Safari 上中断,但在本地运行完美?

java - 循环引用异常

java - 在 Android 中使用 JSON 解析图像 url 和日期

java - JpaSpecificationExecutor<T> 似乎不像 JpaRepository<T, I> 那样工作

java - 带有 JTA 事务的 Spring java 配置

javascript - 在 node.js 中将数组定义为环境变量