Skip to content
Snippets Groups Projects
Commit 9fb93d03 authored by fz2907's avatar fz2907
Browse files

Updated the example controller.

Change spring boot port to 8081, so it will not interoperate with Vue
parent d1b522e2
No related branches found
No related tags found
3 merge requests!27Sprint 1 done,!7Changed DB port from 3306 to 3307. So it will not interrupt with your own local MySQL,!3Changed the example controller and changed spring port to 8081
......@@ -29,7 +29,7 @@ public class ExampleController {
* @return a ResponseModel in String format
*/
@GetMapping
public String getExample() {
public ResponseModel getExample() {
logger.info("You reached the getExample() function.");
return ResponseModel.generateResponse(
......@@ -44,7 +44,7 @@ public class ExampleController {
* @return a ResponseModel in String format
*/
@GetMapping("/{userId}")
public String getExampleWithInput(@PathVariable long userId){
public ResponseModel getExampleWithInput(@PathVariable long userId){
return ResponseModel.generateResponse(
"This this a example String response",
HttpStatus.OK,
......@@ -57,7 +57,7 @@ public class ExampleController {
* @return a ResponseModel in String format
*/
@GetMapping("/detail/{userId}")
public String getExampleDetailWithInput(@PathVariable long userId){
public ResponseModel getExampleDetailWithInput(@PathVariable long userId){
return ResponseModel.generateResponse(
"This this a example String response",
HttpStatus.OK,
......@@ -69,7 +69,7 @@ public class ExampleController {
* @return an example user info in the data field
*/
@RequestMapping("/getUser")
public String getExampleUser(){
public ResponseModel getExampleUser(){
ExampleModel user = new ExampleModel(233, "example-user");
return ResponseModel.generateResponse(
"There is your example user info",
......@@ -83,7 +83,7 @@ public class ExampleController {
* @param newUser the JSON input in the request body
*/
@PostMapping("/newUser")
public String handlePost(@RequestBody ExampleModel newUser){
public ResponseModel handlePost(@RequestBody ExampleModel newUser){
logger.info("You reached the handlePost() function.");
return ResponseModel.generateResponse(
"I received your new user data, check it in data section",
......@@ -97,7 +97,7 @@ public class ExampleController {
* @return info
*/
@DeleteMapping("/logout/{userId}")
public String logout(@PathVariable long userId){
public ResponseModel logout(@PathVariable long userId){
logger.warn("You reached the logout() function.");
return ResponseModel.generateResponse(
"I received your logout request",
......
/**
* This class this a data model example
*/
package vt.CS5934.SwitchRoom.models;
import lombok.Data;
......@@ -11,3 +15,9 @@ public class ExampleModel {
this.userId = userId;
}
}
/*
Change logs:
Date | Author | Description
2022-10-12 | Fangzheng Zhang | create class and init
*/
\ No newline at end of file
package vt.CS5934.SwitchRoom.models;
/**
* This class is ResponseModel for http request response.
*/
import com.google.gson.Gson;
package vt.CS5934.SwitchRoom.models;
import lombok.Data;
import org.springframework.http.HttpStatus;
import java.util.HashMap;
import java.util.Map;
@Data
public class ResponseModel {
Map<String, Object> map;
public static String generateResponse(String message, HttpStatus status, Object data) {
Gson gson = new Gson();
Map<String, Object> map = new HashMap<>();
map.put("message", message);
map.put("status", status.value());
map.put("data", data);
return gson.toJson(map);
String message;
HttpStatus status;
Object data;
public static ResponseModel generateResponse(String message, HttpStatus status, Object data) {
ResponseModel responseModel = new ResponseModel();
//Map<String, Object> map = new HashMap<>();
responseModel.message = message;
responseModel.status = status;
responseModel.data = data;
return responseModel;
}
public ResponseModel(){
map = new HashMap<>();
map.put("message", "");
map.put("status", HttpStatus.NOT_ACCEPTABLE);
map.put("data", null);
message = "";
status = HttpStatus.NOT_ACCEPTABLE;
data = null;
}
public void setMessage(String message){
map.put("message",message);
this.message = message;
}
public void setStatus(HttpStatus status){
map.put("status", status.value());
this.status = status;
}
public void setMessage(Object data){
map.put("data",data);
this.data = data;
}
}
public String build(){
Gson gson = new Gson();
return gson.toJson(map);
}
}
\ No newline at end of file
/*
Change logs:
Date | Author | Description
2022-10-12 | Fangzheng Zhang | create class and init
*/
\ No newline at end of file
/**
* This class will help to send the http request to server side.
*/
const URL = location.protocol +"//"+ location.hostname + ":8081/";
export default{
sendGetRequest(path){
let url = URL + path;
return fetch(url)
.then((response)=>{
if(response.ok){
return
}
})
}
}
/*
Change logs:
Date | Author | Description
2022-10-12 | Fangzheng Zhang | create class and init
*/
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment