【发布时间】: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命令检查它们。 -
您好添加了日志。请帮帮我。