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

"Added example controller and model. Fixed can not start app by comment the DB...

"Added example controller and model. Fixed can not start app by comment the DB library, I will uncomment them when I need."
parent fe083f3d
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,!1Init spring boot, and add few example
......@@ -19,9 +19,9 @@ repositories {
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
// implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
// implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.session:spring-session-core'
compileOnly 'org.projectlombok:lombok'
......@@ -29,6 +29,7 @@ dependencies {
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'com.google.code.gson:gson:2.9.0'
}
tasks.named('test') {
......
/**
* This class is an example class that show the functionalities that spring controller can do.
* You can copy the functions and use in you class.
*/
package vt.CS5934.SwitchRoom.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import vt.CS5934.SwitchRoom.models.ExampleModel;
import vt.CS5934.SwitchRoom.models.ResponseModel;
/**
* The "@RestController" made the class into rest handle class
* The "@RequestMapping("example")" on the class level make it only react to url ".../example/..."
*/
@RestController
@RequestMapping("example")
public class ExampleController {
@GetMapping("/example")
public String getExample(){
return "This this a example String response";
/**
* You can use logger.[trace,debug,info,warn,error]("messages") to log into file
*/
Logger logger = LoggerFactory.getLogger(ExampleController.class);
/**
* This function handles GET request on url "/example"
* @return a ResponseModel in String format
*/
@GetMapping
public String getExample() {
logger.info("You reached the getExample() function.");
return ResponseModel.generateResponse(
"This this a example String response",
HttpStatus.OK,
"Some Json Object Later");
}
/**
* This function handles GET request on url "/example/<a-long-val>"
* @param userId this variable will be passed in the url path
* @return a ResponseModel in String format
*/
@GetMapping("/{userId}")
public String getExampleWithInput(@PathVariable long userId){
return ResponseModel.generateResponse(
"This this a example String response",
HttpStatus.OK,
"You GET this function with id = "+ userId);
}
/**
* This function handles GET request on url "/example/detail/<a-long-val>"
* @param userId this variable will be passed in the url path
* @return a ResponseModel in String format
*/
@GetMapping("/detail/{userId}")
public String getExampleDetailWithInput(@PathVariable long userId){
return ResponseModel.generateResponse(
"This this a example String response",
HttpStatus.OK,
"You GET this function with detail id: "+ userId);
}
/**
* This function handles GET request on url "/example/getUser"
* @return an example user info in the data field
*/
@RequestMapping("/getUser")
public String getExampleUser(){
ExampleModel user = new ExampleModel(233, "example-user");
return ResponseModel.generateResponse(
"There is your example user info",
HttpStatus.OK,
user);
}
/**
* This function will handle the post function and change the JSON body into data class
* @param newUser the JSON input in the request body
*/
@PostMapping("/newUser")
public String 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",
HttpStatus.OK,
newUser);
}
/**
* If you need to delete anything from DB, use DELETE request
* @param userId user id
* @return info
*/
@DeleteMapping("/logout/{userId}")
public String logout(@PathVariable long userId){
logger.warn("You reached the logout() function.");
return ResponseModel.generateResponse(
"I received your logout request",
HttpStatus.OK,
null);
}
}
/*
Change logs:
Date | Author | Description
2022-10-11 | Fangzheng Zhang | create class and init
*/
package vt.CS5934.SwitchRoom.models;
import lombok.Data;
@Data
public class ExampleModel {
Long userId;
String username;
public ExampleModel(long userId, String name) {
this.username = name;
this.userId = userId;
}
}
package vt.CS5934.SwitchRoom.models;
import com.google.gson.Gson;
import org.springframework.http.HttpStatus;
import java.util.HashMap;
import java.util.Map;
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);
}
public ResponseModel(){
map = new HashMap<>();
map.put("message", "");
map.put("status", HttpStatus.NOT_ACCEPTABLE);
map.put("data", null);
}
public void setMessage(String message){
map.put("message",message);
}
public void setStatus(HttpStatus status){
map.put("status", status.value());
}
public void setMessage(Object data){
map.put("data",data);
}
public String build(){
Gson gson = new Gson();
return gson.toJson(map);
}
}
\ 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