java - 如何使用 Spring 设置 RESTful API 以使用 Controller 和 Model 发送 JSON 响应
问题描述
我正在尝试RESTful
使用 Spring 设置 API,以便我可以发送HTTP
POST 请求并接收响应。
我正在测试的 post 请求看起来完全像这样:
POST http://localhost:8081/decide
Accept: application/json
Cache-Control: no-cache
Content-Type: application/json
{
"previousDecisions": [0, 1, 2]
}
当我发送该请求(我正在使用IntelliJ's
“测试RESTful
Web 服务”功能)时,我收到了错误响应。
我想要发生的是,有一个Decide
用构造函数调用的类,它只需要一个整数数组,它唯一的成员变量是一个整数数组。我想将上述请求传递DecideController
给创建Decide
. 然后Decide
有一个返回Node
对象的方法。我想返回该Node
实例的 JSON 版本。
从我在 Spring 中看到的情况来看,它在返回时会自动将 Java 对象转换为 JSON 格式。
这是我的整个决定课:
package com.thesisapp.server.jugtours.model;
import java.io.Serializable;
public class Decide implements Serializable {
private int[] decisionList;
public Decide() {
this.decisionList = new int[0];
}
public Decide(int[] decisionList) {
this.decisionList = decisionList;
}
public Node getNode() {
//Use the game class to get a root node for the entire story tree
Node rootNode = (new Game()).getFullStoryTree();
//Traverse the story tree using the decisionList to get to the current node
Node currentNode = rootNode;
for (int whichChild : this.decisionList) {
currentNode = currentNode.getChild(whichChild);
}
return currentNode;
}
}
旁注:我只是implements Serializable
在做一些研究后添加了它,它对我收到的错误消息没有影响。但是,添加该默认构造函数确实会对错误消息产生影响。
这是我的整个 DecideController 类
package com.thesisapp.server.jugtours.web;
import com.thesisapp.server.jugtours.model.Node;
import com.thesisapp.server.jugtours.model.Decide;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@RestController
public class DecideController {
@PostMapping("/decide")
public Node decide(@RequestBody Decide decide) {
return decide.getNode();
}
}
同样,这是我发送的请求:
POST http://localhost:8081/decide
Accept: application/json
Cache-Control: no-cache
Content-Type: application/json
{
"previousDecisions": [0, 1, 2]
}
这是我希望响应的样子,只是Node
类中所有成员变量的 JSON 形式:
{
"id": 0,
"text": "some text here",
"decisions": [],
"children": [],
"speaker": 0,
"checkpoint": true
}
这是我设法得到的两个响应(在将默认构造函数添加到Decide
类之前和之后)
{
"timestamp": "2019-09-04T02:31:05.762+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Type definition error: [simple type, class com.thesisapp.server.jugtours.model.Decide]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.thesisapp.server.jugtours.model.Decide` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 2, column: 3]",
"path": "/decide"
}
{
"timestamp": "2019-09-04T01:50:03.990+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No message available",
"path": "/decide"
}
我真的不知道这里出了什么问题。如果我没有提供足够的信息/代码,请告诉我。我对这东西的经验很少,但我很感激任何帮助,谢谢!
解决方案
您需要decisionList
在Decide
类上使用 getter/setter,以便杰克逊可以正确绑定到它。此外,您的 JSON 请求正文不会映射到您定义的 Decide 对象。通过传入
{
"previousDecisions": [0, 1, 2]
}
您的 Decide 类应如下所示:
public class Decide {
private int[] previousDecisions;
public int[] getPreviousDecisions(){
return previousDecisions;
}
public void setPreviousDecisions(int[] previousDecisions){
this.previousDecisions = previousDecisions;
}
}
因此,要么将字段名称Decide
从decisionList
to更改为previousDecisions
并添加 getter/setter,要么decisionList
作为 JSON 中的属性传递并将 getter/setter 添加到类中:
{
"decisionList": [0, 1, 2]
}
推荐阅读
- ruby-on-rails - 如何将信息添加到自定义用户字段(设计)
- reactjs - 如何改进 ReactJs 的构建错误报告?
- r - 使用 glmmTBM 对 beta-binomial 分布式数据进行建模
- html - 如何编辑嵌入的 iframe?
- powershell - 在 robocopy 期间遍历 samba 共享并排除一些
- python - 条件格式 Python
- java - 全屏浏览 Web 视频
- react-native - React Native Modal VS 屏幕
- sql - 如何根据用户对具有特定条件的特定记录的权限有效地过滤大量记录?
- android - Recyclerview 实际上并不回收视图?