繁体   English   中英

如何使用 Spring 设置 RESTful API 以使用 Controller 和 Model 发送 JSON 响应

[英]How to set up a RESTful API with Spring to send a JSON response with Controller and Model

我正在尝试使用 Spring 设置RESTful 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 格式。

这是我的整个 Decide 课程:

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 Serializableimplements 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"
}

我真的不知道这里出了什么问题。 如果我没有提供足够的信息/代码,请告诉我。 我对这些东西的经验很少,但我很感激任何帮助,谢谢!!

您需要在Decide类上的decisionList getter/setter 方法,以便 Jackson 可以正确绑定到它。 此外,您的 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更改为previousDecisions并添加 getter/setter,要么将decisionList作为 JSON 中的属性传递,并将 getter/setter 添加到类中:

{   
   "decisionList": [0, 1, 2]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM