Last active
February 3, 2018 11:13
-
-
Save chatton/3f259344f68635d2ea916d2c6ef4ada7 to your computer and use it in GitHub Desktop.
Example of how to send a get and post request that works with a Spring Boot application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ie.gmit.sw.controllers; | |
/* | |
POJO class, just needs an empty constructor | |
and getters and setters. | |
returned as | |
{ | |
"name": name, | |
"id" : id | |
} | |
*/ | |
public class Message { | |
private String name; | |
private int id; | |
public Message() { | |
} | |
Message(String name, int id) { | |
this.name = name; | |
this.id = id; | |
} | |
public void inc() { | |
id++; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this polling function will query the server every 5 | |
// seconds, we can use this to continually update game state. | |
// small test to test connectivity to spring boot server. | |
function poll() { | |
console.log("Polling..."); | |
/* | |
get will be used to get a new board | |
*/ | |
$.get("/test", data => { | |
// data is valid JS object, not a string. | |
// test data. | |
console.log("From GET"); | |
console.log(data.name); | |
console.log(data.id); | |
}); | |
/* | |
post will be used to send moves. A1 -> B3 etc. | |
*/ | |
$.ajax({ | |
type: "POST", | |
contentType: "application/json", // this is required by spring boot. Otherwise get a 415 error. | |
data: JSON.stringify({id: 10, name: "Test Post Name"}), // send object as string to server | |
url: window.location + "games", | |
success: e => { | |
console.log(e); | |
}, | |
error: e => { | |
console.log(e); | |
} | |
}); | |
setTimeout(poll, 5000) | |
} | |
setTimeout(poll, 5000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ie.gmit.sw.controllers; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.RequestBody; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
// needs to be marked as a RestController to provide endpoints | |
@RestController | |
public class TestController { | |
/* if you navigate to localhost:8080/test you get the json response | |
{ | |
"name" : "Hello World", | |
"id" : 123 | |
} | |
*/ | |
@RequestMapping("/test") | |
public ResponseEntity<Message> test() { | |
return new ResponseEntity<>(new Message("Hello World", 123), HttpStatus.OK); | |
} | |
/* | |
a post request of an object with a name and id will be converted | |
into a Message object with those attributes. | |
*/ | |
@PostMapping(value = "/games") | |
public ResponseEntity<Message> addOne(@RequestBody Message message) { | |
message.inc(); | |
return ResponseEntity.ok(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment