How to Set Up a RESTful API with Spring

Kyle Hobbs :

I am trying to set up a RESTful API with Spring, using react for the front-end. The client is meant to send a POST request that looks something like this:

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

Then the server will respond with an Object called Node which looks like this after being converted to JSON:

{
  "id": 0,
  "text": "Something here...",
  "decisions": [],
  "children": [],
  "speaker": 0,
  "checkpoint": true
}

I am struggling to follow some of the example code I have seen for how to set this up using Spring.

The way the back end is set up is:

There is a Decide class, a Node class, and a DecideController class. DecideController is supposed to take a POST request like above and then use the Decide class to get an instance of the Node class and use it as a response to the client.

I haven't begun testing the client side of things yet, but I am using Intellij Restful API tool to check if its working and it is giving me an error. Heres what I have for the actual method which should be handling the POST Request:

@RestController
public class DecideController {

  @PostMapping("/decide")
  public Node decide(@Valid @RequestBody Decide decide) {
    return decide.getNode();
  }
}

I am getting an error response when I send a request with this body:

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

And with these headers:

Accept: application/json
Cache-Control: no-cache

This is the error response:

{"timestamp":"2019-09-01T23:54:58.037+0000","status":500,"error":"Internal Server Error","message":"Content-Type cannot contain wildcard type '*'","path":"/decide"}

The last thing I can think of that you might need to help me out is the Decide class and it is pretty short so I will include that here as well:

public class Decide {

  private int[] decisionList;

  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;
  }
}

As said about what I am expecting from that POST request is a response that looks like this:

{
  "id": 0,
  "text": "Something here...",
  "decisions": [],
  "children": [],
  "speaker": 0,
  "checkpoint": true
}

Sorry I am pretty new to all of this so hopefully everything I said here makes sense, but if not I am happy to clarify or provide more information. Thank you!!

paulsm4 :

The error is coming from the server, because of the client:

"Internal Server Error","message":"Content-Type cannot contain wildcard type '*'"

Even though the PAYLOAD looks ok, your react client is setting the Content-Type HTTP header to "*". You need to fix the problem in react.

SUGGESTION: Follow this link to set Content-Type to application/json.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=144248&siteId=1