1

I am creating an API that receives the following input, and provides the following output. enter image description here

I have already created a working method for "new":

@RequestMapping(value = "/new", method = RequestMethod.GET) public StartedGame startGame(HttpSession session){ List<Game> games = getCurrentGames(session); Game newGame = new Game(wordList); games.add(newGame); return new StartedGame(newGame); } 

Which returns the following JSON:

{ "gameId": "kvmuyw", "word": "_______" } 

However, I need to create a function for making guesses. I have not had any luck. I have this as my function header, however it does not seem to be correct...

@RequestMapping(value = "/guess", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") public Game makeGuess(@RequestBody String json, HttpSession session) 
5
  • 2
    it does not seem to be correct...: why? What are you doing? What do you expect to happen? What happens instead? Be precise. Note that the whole point of JSON is that it can be mapped to objects. Why take a String json as argument and parse it yourself instead of taking a Guess object as argument and let Spring (Jackson in fact) map the JSON to the Guess object? Commented Apr 27, 2018 at 19:58
  • I get a 404 error. What I am expecting to happen is above in the picture. Commented Apr 27, 2018 at 20:04
  • What URL did you hit? What json did you send? How did you send it? When your application started did you see in the logs that it mapped the /guess endpoint? Commented Apr 27, 2018 at 20:06
  • @JBNizet Here is some output, with explanation: stackoverflow.com/questions/50071720/… Commented Apr 28, 2018 at 0:28
  • @ohbrobig you said you get a 404 error, then in that question you say in the title you get 415 error and the output you've pasted says 500 error... Commented Apr 28, 2018 at 7:16

1 Answer 1

1

You probably want something like

@RequestMapping(value = "/guess", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") public Game makeGuess(@RequestBody Guess guess){ // .. } @Data // <- this assuming you're using Lombok - add required accessors if not public class Guess { String game; String guess; } 

However, if you're getting 404 Not Found, your problems are not with method definition, but that you're posting to wrong URL.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried using a Guess object (with getters and setters)...it’s possible I am posting to wrong URL. With the above method, what would be the appropriate URL?
Assume the JSON object is similar to the ones in the picture above.
@ohbrobig right url is /guess, assuming you don't have additional mappings at controller level or application. If you have, it would be /controllerurlvalue/guess or /applicationurlvalue/controllerurlvalue/guess. You can see the registered urls from the logs when spring app is started.
@ohbrobig if you get error 500 with nullpointerexception like you claim here, it means at least that your url mapping is working, so apparently you got this to work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.