我正在开发一个项目,角度6作为前端,弹簧启动作为REST后端。 (英语不是我的母语所以,我提前为语法道歉。)
一切都很好,直到我在某些方法中出错。
我正在从我的前端服务发出一个http.post请求,并从spring引导中收到错误,说不允许使用内容类型的应用程序/文本(这不适用于其他PostMapping方法,但无论如何)我设法更改了帖子标题使用此代码:
persistGame(game: Game) {
const path = 'http://localhost:8080/games';
const headers = new Headers();
headers.set('Content-Type', 'application/json; charset=utf-8');
const options = new RequestOptions();
options.headers = headers;
console.log(game);
return this.http.post(path, Game, options);
}
然后,console.log显示正确的对象,但我的后端开始抱怨""timestamp":"2018-09-05T20:11:04.848+0000","status":400,"error":"Bad Request","message":"JSON parse error: Unrecognized token 'function': was expecting 'null', 'true', 'false' or NaN; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'function': was expecting 'null', 'true', 'false' or NaN\n at [Source: (PushbackInputStream); line: 1, column: 10]","path":"/games"}"
当我检查有效载荷时,我发现了这个
"function Game() {
}"
而不是我的对象....
我的后端方法如下所示:
@CrossOrigin()
@PostMapping(path = "/games")
public ResponseEntity<Object> createGame(@RequestBody Game game) {
ResponseEntity<Object> output;
String body;
Game gameInBD;
gameInBD = gameService.findByName(game);
if (gameInBD == null)
{
gameService.saveGame(game);
} else
{
/* updates gameInDB fields with game fields and persist gameInDB */
}
output= ResponseEntity.ok(game);
return output;
我怎样才能避免从对象到函数的这种转换?
您将该类作为参数发送到帖子
它应该是这样的'return this.http.post(path,game,options);'
用小写游戏