Java 在http异常中返回自定义消息
这是我的ExceptionHandler类Java 在http异常中返回自定义消息,java,spring,spring-boot,exception-handling,Java,Spring,Spring Boot,Exception Handling,这是我的ExceptionHandler类 @ResponseStatus(HttpStatus.NOT_FOUND) public class NotFoundException extends Exception { private static final long serialVersionUID = -1964839652066902559L; public NotFoundException(String message) { super(mess
@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFoundException extends Exception {
private static final long serialVersionUID = -1964839652066902559L;
public NotFoundException(String message) {
super(message);
}
}
在我的服务中,我有
@Service
public class myService {
public String getName() throws NotFoundException{
logger.error("Name not found");
throw new NotFoundException("Name not found");
}
}
我得到的答复是:
{
"timestamp": 1486967853916,
"status": 404,
"error": "Not Found",
"exception": "com.exceptions.NotFoundException",
"message": "Not Found",
"path": "/names"
}
我只希望我的消息从服务中传递并在异常中返回。但这并没有发生。谁能告诉我怎么做。使用@ControllerAdvice和@ExceptionHandler的组合,也许这会对你有所帮助
@ControllerAdvice
public class GlobalValidationResponse extends ResponseEntityExceptionHandler {
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<?> handleEntityNotFoundException(NotFoundException ex, HttpServletRequest request, HttpServletResponse response) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ValidationError(ex.getMessage()));
}
}
@ControllerAdvice
公共类GlobalValidationResponse扩展了ResponseEntityExceptionHandler{
@ExceptionHandler(NotFoundException.class)
public ResponseEntity handleEntityNotFoundException(NotFoundException ex、HttpServletRequest请求、HttpServletResponse响应){
返回ResponseEntity.status(HttpStatus.NOT_FOUND).body(新的ValidationError(例如getMessage());
}
}
ValidationError的实体
public class ValidationError {
//@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Map<String, String> errors = new HashMap<>();
private String errorMessage;
public ValidationError() {
super();
}
public ValidationError(String errorMessage) {
this.errorMessage = errorMessage;
}
public ValidationError(Map<String, String> errors) {
this.errors = errors;
}
public Map<String, String> getErrors() {
return errors;
}
public void setErrors(Map<String, String> errors) {
this.errors = errors;
}
public String getErrorMessage() {
return errorMessage;
}
}
公共类验证错误{
//@JsonInclude(JsonInclude.Include.NON_EMPTY)
私有映射错误=新HashMap();
私有字符串错误消息;
公共验证错误(){
超级();
}
公共验证错误(字符串错误消息){
this.errorMessage=errorMessage;
}
公共验证错误(映射错误){
这个。错误=错误;
}
公共映射getErrors(){
返回错误;
}
公共void setErrors(映射错误){
这个。错误=错误;
}
公共字符串getErrorMessage(){
返回错误消息;
}
}
公共类myService抛出NotFoundException
您能够编译这个吗?你是怎么得到回应的?是的。我还没有输入控制器代码。我的管制员,谢谢你纠正这个问题。仅供参考:我没有投票否决你的问题。但是我如何明确地从我的服务中抛出这个异常,因为你已经在服务中抛出了——抛出new NotFoundException(“名称未找到”);{“timestamp”:1486971408978,“status”:500,“error”:“Internal Server error”,“exception”:“javassist.NotFoundException”,“message”:“org.springframework.web.util.NestedServletException:请求处理失败;嵌套的异常是javassist.NotFoundException:vfcd”,“path”:“/names”}我得到的状态代码是500。我需要它是404500意味着这是一个内部服务器错误。返回ResponseEntity.status(HttpStatus.NOT_FOUND).body(新的ValidationError(例如getMessage());