我的 REST 應用程式有問題。這是減輕您負擔的 git 鏈接:https ://github.com/TheNiceGuy123/AstonishingBackEnd
這里也是原始代碼:
按順序發布的課程:
員工控制器:
package com.example.csmartbackend.controller;
import com.example.csmartbackend.model.Employee;
import com.example.csmartbackend.modeldto.EmployeeDto;
import com.example.csmartbackend.service.EmployeeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;
@RestController
@RequestMapping("employee")
@RequiredArgsConstructor
public class EmployeeController
{
private final EmployeeService employeeService;
@GetMapping("find/{Cnp}")
public ResponseEntity<Employee> findByCNP(@PathVariable("Cnp") String Cnp)
{
Employee employee = employeeService.findByCnp(Cnp);
HttpHeaders header = new HttpHeaders();
header.add("Desc","Getting employee by id.");
return ResponseEntity.status(HttpStatus.OK).headers(header).body(employee);
}
}
員工服務:
package com.example.csmartbackend.service;
import com.example.csmartbackend.mapper.EmployeeMapper;
import com.example.csmartbackend.model.Employee;
import com.example.csmartbackend.modeldto.EmployeeDto;
import com.example.csmartbackend.repository.EmployeeRepository;
import exception.general.TargetNotFoundException;
import exception.requestsexceptions.CNPNotFoundException;
import exception.requestsexceptions.EmployeeNotFoundException;
import exception.requestsexceptions.InvalidIdException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Service
@RequiredArgsConstructor
@Transactional
public class EmployeeService implements EmployeeServiceImpl
{
private final EmployeeRepository employeeRepository;
private final ContractService contractService;
private final AdressService adressService;
public Employee findByCnp(String Cnp) throws CNPNotFoundException
{
Optional<Employee> employeeOptional = Optional.ofNullable(employeeRepository.findByCnp(Cnp));
if(employeeOptional.isPresent())
return employeeOptional.get();
else
throw new CNPNotFoundException("No employee found.");
}
}
全域例外處理程式:
package exception.requestsexceptions;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler
{
@Override
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported
(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
{
String message = ex.getMessage();
List<String> details = new ArrayList<>();
details.add("Request method not supported.");
ApiErrorModel apiErrorModel = new ApiErrorModel(message, details, status, LocalDateTime.now());
return ResponseEntity.status(status).body(apiErrorModel);
}
@Override
protected ResponseEntity<Object> handleHttpMediaTypeNotSupported
(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
{
String message = ex.getMessage();
List<String> details = new ArrayList<>();
details.add("Media method not supported.");
ApiErrorModel apiErrorModel = new ApiErrorModel(message, details, status, LocalDateTime.now());
return ResponseEntity.status(status).body(apiErrorModel);
}
@Override
protected ResponseEntity<Object> handleMissingPathVariable
(MissingPathVariableException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
{
String message = ex.getMessage();
List<String> details = new ArrayList<>();
details.add("Path variable is missing.");
ApiErrorModel apiErrorModel = new ApiErrorModel(message, details, status, LocalDateTime.now());
return ResponseEntity.status(status).body(apiErrorModel);
}
@Override
protected ResponseEntity<Object> handleMissingServletRequestParameter
(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
{
String message = ex.getMessage();
List<String> details = new ArrayList<>();
details.add("Request parameter is missing.");
ApiErrorModel apiErrorModel = new ApiErrorModel(message, details, status, LocalDateTime.now());
return ResponseEntity.status(status).body(apiErrorModel);
}
@Override
protected ResponseEntity<Object> handleTypeMismatch
(TypeMismatchException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
{
String message = ex.getMessage();
List<String> details = new ArrayList<>();
details.add("Mismatch of type.");
ApiErrorModel apiErrorModel = new ApiErrorModel(message, details, status, LocalDateTime.now());
return ResponseEntity.status(status).body(apiErrorModel);
}
@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable
(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
{
String message = ex.getMessage();
List<String> details = new ArrayList<>();
details.add("Request body is not readable.");
ApiErrorModel apiErrorModel = new ApiErrorModel(message, details, status, LocalDateTime.now());
return ResponseEntity.status(status).body(apiErrorModel);
}
@ExceptionHandler(EmployeeNotFoundException.class)
public ResponseEntity<Object> handleEmployeeNotFoundException(EmployeeNotFoundException ex)
{
String message = ex.getMessage();
List<String> details = new ArrayList<>();
details.add("Employee not found.");
ApiErrorModel apiErrorModel = new ApiErrorModel(message, details, HttpStatus.BAD_REQUEST, LocalDateTime.now());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(apiErrorModel);
}
@ExceptionHandler(CNPNotFoundException.class)
public ResponseEntity<Object> handleInvalidCNPException(CNPNotFoundException ex)
{
String message = ex.getMessage();
List<String> details = new ArrayList<>();
details.add("Employee with the given CNP not found.");
ApiErrorModel apiErrorModel = new ApiErrorModel(message, details, HttpStatus.NOT_FOUND, LocalDateTime.now());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(apiErrorModel);
}
}
CNPNotFoundException 類:
package exception.requestsexceptions;
public class CNPNotFoundException extends RuntimeException
{
public CNPNotFoundException(String message) { super(message); }
}
ApiErrorModel 類:
package exception.requestsexceptions;
import lombok.*;
import org.springframework.http.HttpStatus;
import java.time.LocalDateTime;
import java.util.List;
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class ApiErrorModel
{
String message;
List<String> details;
HttpStatus status;
LocalDateTime timestamp;
}
我想像第二張圖片一樣向用戶提供一些相關資訊,但是在郵遞員中,所有內容都保持在默認級別以像這樣呼叫它,就像我的處理程式中的代碼甚至不存在一樣。有什么我想念的邏輯或注釋嗎?
這就是我這邊的樣子
這就是它的樣子
感謝您的時間!
uj5u.com熱心網友回復:
問題是當一個類被注釋時@SpringBootApplication沒有任何屬性 SpringBoot 只掃描類本身所在的包及其子包。
在您的情況下,這意味著 SpringBoot 僅com.example.csmartbackend在您將GlobalExceptionHandler類放入exception.requestsexceptions 包時查找包下的任何 Spring 組件。
要修復它,您有兩種選擇:
- 將
exception包放在 com.example.csmartbackend 下,這樣就可以了com.example.csmartbackend.exception.requestsexception.GlobalExceptionHandler exception通過使用scanBasePackageClasses或scanBasePackages屬性告訴spring也掃描包,例如@SpringBootApplication(scanBasePackageClasses = {CSmartBackEndApplication.class, GlobalExceptionHandler.class})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/464103.html
