我在sping-boot
自定义异常处理方面遇到了很多困难。客户异常不会被异常处理程序捕获。REST API适用于有效的请求有效负载。当有错误时,我正在尝试发送错误响应。但是错误响应总是为空,状态代码为200而不是404。
控制器
package com.company.paypage.v2.controller;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.company.paypage.exception.*;
import com.company.paypage.model.ErrorMessageConstants;
import com.company.paypage.v2.model.ConfigPayload;
import com.company.paypage.v2.model.ConfigResponse;
import com.company.paypage.v2.services.FeatureConfigService;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping(value = "v2/setup")
public class FeatureConfigController {
@Autowired
private FeatureConfigService featureconfigService;
/*
features config endpoint
*/
@RequestMapping(value = "/config", method = POST, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ConfigResponse setupConfigRequest(@Valid @RequestBody ConfigPayload payload, HttpServletRequest request, HttpServletResponse servResponse) {
log.info("Processing the feature config request for " + payload.getPage_type());
ConfigResponse response = null;
try {
response = featureconfigService.processConfigRequest(payload);
System.out.println(response);
if(response == null) {
throw new FeatureConfigException(ErrorMessageConstants.NOT_FOUND, "Error while generating feature config response.....");
}
} catch (FeatureConfigException e){
log.error("Exception:", e);
}
return response;
}
}
异常类
package com.company.paypage.exception;
public class FeatureConfigException extends Exception {
String code;
String message;
public FeatureConfigException(String code, String message) {
super(message);
this.code = code;
this.message = message;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
< code >异常处理程序
package com.company.paypage.exception;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;
import com.company.paypage.model.ApplicationConstants;
import com.company.paypage.model.ErrorCodeConstants;
import com.company.paypage.model.ErrorMessageConstants;
import com.company.paypage.model.GeneralErrorInfo;
import com.company.paypage.model.Payment;
import com.company.paypage.model.SetupResponse;
import com.company.paypage.v2.model.ConfigResponse;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@ControllerAdvice
public class GeneralExceptionHandler{
@ExceptionHandler(FeatureConfigException.class)
protected ResponseEntity<ConfigResponse> handleFeatureConfigException(FeatureConfigException ex, HttpServletRequest request){
GeneralErrorInfo generalErrorInfo = new GeneralErrorInfo().withCode(ex.getCode());
generalErrorInfo.setMessage(ex.getMessage());
String referenceId =(String) request.getAttribute(ApplicationConstants.REFERENCE_ID);
ConfigResponse configResponse = buildConfigResponse(generalErrorInfo, referenceId);
log.error("{} {}-{}"
, ex.getMessage()
, request.getHeader(ApplicationConstants.X_GP_REQUEST_ID)
, referenceId
, ex);
return new ResponseEntity<ConfigResponse>(configResponse, addCustomerHeaders(request), HttpStatus.BAD_REQUEST);
}
@SuppressWarnings("null")
ConfigResponse buildConfigResponse(GeneralErrorInfo generalErrorInfo, String referenceId) {
ConfigResponse configResponse = new ConfigResponse();
configResponse.setError(generalErrorInfo);
configResponse.setAutocomplete((Boolean) null);
configResponse.setRefund((Boolean) null);
configResponse.setSplit_payment((Boolean) null);
configResponse.setTender_type(null);
configResponse.setVoidd((Boolean) null);
return configResponse;
}
}
配置响应模型
package com.company.paypage.v2.model;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.company.paypage.model.GeneralErrorInfo;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"tender_type",
"autocomplete",
"split_payment",
"refund",
"void",
"error"
})
public class ConfigResponse implements Serializable {
@JsonProperty("tender_type")
private TenderType tender_type;
@JsonProperty("autocomplete")
private boolean autocomplete;
@JsonProperty("split_payment")
private boolean split_payment;
@JsonProperty("refund")
private boolean refund;
@JsonProperty("void")
private boolean voidd;
@JsonProperty("error")
private GeneralErrorInfo error;
public TenderType getTender_type() {
return tender_type;
}
public void setTender_type(TenderType tender_type) {
this.tender_type = tender_type;
}
public boolean isAutocomplete() {
return autocomplete;
}
public void setAutocomplete(boolean autocomplete) {
this.autocomplete = autocomplete;
}
public boolean isSplit_payment() {
return split_payment;
}
public void setSplit_payment(boolean split_payment) {
this.split_payment = split_payment;
}
public boolean isRefund() {
return refund;
}
public void setRefund(boolean refund) {
this.refund = refund;
}
public boolean isVoidd() {
return voidd;
}
public void setVoidd(boolean voidd) {
this.voidd = voidd;
}
public GeneralErrorInfo getError() {
return error;
}
public void setError(GeneralErrorInfo error) {
this.error = error;
}
public ConfigResponse withError(GeneralErrorInfo error) {
setError(error);
return this;
}
}
这里可能有什么问题?在<code>JSON<code>格式的响应中,我缺少什么来获取正确的错误?
您必须定义异常的代码。在这里,您可以找到如何处理REST异常的不同变体:链接
imi 框架底层支持将错误转为异常,可以通过 try...catch 来捕获。 默认是不启用的,你可以通过配置来设定错误捕获的等级,并且启用它: 在 config.php 中的 beans 配置 [ 'ErrorLog' => [ 'level' => E_ERROR | E_WARNING | E_PARSE, // 报告 runtime 错误 //
问题内容: 我试图将自定义错误消息返回给用户,以让他们知道发生错误时出了什么问题,但是我已经尝试了所有方法来显示该消息,并且似乎什么也没有捕捉到。这是我的角度代码: 这是它正在调用的 styleAPIservice 函数: 这是 API 函数: 这是发生错误时返回的内容(例如Style已经存在): 我究竟做错了什么?我觉得自己到处都在搜索,并尝试了所有可能的建议,但是对于为什么我无法显示错误消息我
我正在寻找一种方法来处理与前端的通信,以防响应状态(ResponseStatus)导致的错误,从而 如果回复是400/401/403,我可以记录信息并发送特定消息 如果响应为500,我可以记录错误并发送另一条特定消息 等等 在我们的Api网关中,我们有一个(),我们在其中处理异常并将自己的DTO返回给前端。 如果我以单个异常为目标(参见下面的示例),那么一切都可以正常工作,但如果我只想以响应HTT
我使用的是SpringBoot2.3。我遇到了一些例外情况。我想使用类在全局级别捕获异常。我能够捕获验证错误并返回自定义错误响应,但Spring似乎忽略了我的方法。这是我的课程: andler.java 下面是我抛出异常的方法: PhotoStorageServiceImp.java PhotoUploadController.java } 我在检索照片时遇到错误
Twilio新手使用测试帐户。我按照这里列出的安装Twilio PHP的说明进行了安装:https://www.Twilio.com/docs/quickstart/php/sms 因为我得到了一个证书错误,所以我的主机提供程序建议我更改CURLOPT_SSL_VERIFYPEER=>false(从true改为true)。但现在我得到了这个错误。如何修复?:致命错误:未捕获异常“services_
我在Eclipse中尝试Apache HTTP客户端库 下面的代码片段检查了异常并需要处理。 日蚀给出了3条建议 > Add throws Exception-(工作正常) 用尝试捕捉包围- (也工作正常) 用try/multicatch环绕 第三个选项给出错误 客户端协议异常已经被替代的IO异常捕获 我看到了的源代码,它。据我所知,在捕获多个异常时,我们可以在更具体的异常下捕获更一般的异常。因此