[[oktatas:web:back-end_framework:spring_boot|< Spring boot]]
====== Azonosítás ======
* **Szerző:** Sallai András
* Copyright (c) Sallai András, 2023
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]]
* Web: https://szit.hu
===== DTO osztály létrehozása =====
package lan.zold.emp;
public class EmployeeResponse {
public Employee employee;
public String success;
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String getSuccess() {
return this.success;
}
public void setSuccess(String success) {
this.success = success;
}
}
===== Dolgozó hozzáadása =====
A DTO objektum elkészítése:
//...
EmployeeResponse res = new EmployeeResponse();
res.setEmployee(addedEmp);
res.setSuccess("Ok");
return res;
A teljes metódus:
@CrossOrigin
@PostMapping(path="/employees")
public EmployeeResponse store( // <-- változás
@RequestBody Employee emp,
@RequestHeader(
value="Authorization",
required=false)
String tokenHeader) {
String authOkStr = customProperties.getAuth();
boolean authOk = Boolean.parseBoolean(authOkStr);
Employee addedEmp = null;
if(authOk) {
String token = tokenHeader.replace("Bearer ", "");
AuthController authController = new AuthController();
try {
String tokenOk = authController.checkToken(token);
if(tokenOk.equals("tokenok")) {
addedEmp = empRepository.save(emp);
}else {
String msg = "Hiba! A token nem megfelelő!";
throw new IllegalArgumentException(msg);
}
} catch (Exception e) {
System.err.println("Hiba! A token nem jó!");
}
}else {
addedEmp = empRepository.save(emp);
}
/* |
v változás */
EmployeeResponse res = new EmployeeResponse();
res.setEmployee(addedEmp);
res.setSuccess("Ok");
return res;
}