如何通过自己的 REST 服务将 API 异常输出传递给?
How to pass API exception output to through own REST service?
总结:
我想通过使用我自己的 Rest 服务将一个 REST 服务端点给出的有效异常输出传递给最终用户。
我所做的是,我使用 RestTemplate class 在服务 class 中调用了该服务,它根据有效 post 请求提供有效输出。但是当我向它传递无效输入时,我在我的服务 class 中只得到“400 BAD REQUEST”结果,我称之为 API。但是当我使用 postman 分别调用 API 时,我得到了预期的输出。
代码示例:
class Abc {
ResponseEntity<String> = response;
static final String url = "https://abc-xyz.com/client-rest-end-point-url";
public ResponseEntity getDetails(RequestInput requestInput) {
try{
response=restTemplate.postForObject(url,requestInput,String.class);
} catch(Exception e) {
ResponseEntity response = (ResponseEntity<ErrorModel>)restTemplate.postForEntity(url,requestInput,ErrorModel.class);
}//try-catch
}//getDetails method
}//class
使用@ExceptionHandler 注释来注释您的方法。您可以从控制器中单独编码 class。
@ControllerAdvice
public class YourExceptionHandler {
@ExceptionHandler(CustomException.class)
public String xException() {
return "error/exception";
}
}
您可以为整个应用程序创建自定义异常 class,并且可以使用 throw
关键字在 JSON
中发送数据
假设你有例外 class 是:
public class TestException extends Exception {
private static final long serialVersionUID = 1L;
private String code;
private String detailMessage;
public TestException() {
};
public TestException(String message, String code, String detailMessage) {
super(message);
this.code = code;
this.detailMessage = detailMessage;
}
public TestException(String message, String code) {
super(message);
this.code = code;
}
//TestExceptionResponseCode is another class for message data, if required.
public TestException(TestExceptionResponseCode testExceptionResponseCode) {
super(testExceptionResponseCode.getMessage());
this.code = testExceptionResponseCode.getCode();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDetailMessage() {
return detailMessage;
}
public void setDetailMessage(String detailMessage) {
this.detailMessage = detailMessage;
}
}
现在在你的情况下抛出异常可以是这样的:
class Abc {
ResponseEntity<String> = response;
static final String url = "https://abc-xyz.com/client-rest-end-point-url";
public ResponseEntity getDetails(RequestInput requestInput) {
if(requestInput==null){
throw new TestException("FAILED", "1", "Data can't be null");
}
}
总结: 我想通过使用我自己的 Rest 服务将一个 REST 服务端点给出的有效异常输出传递给最终用户。
我所做的是,我使用 RestTemplate class 在服务 class 中调用了该服务,它根据有效 post 请求提供有效输出。但是当我向它传递无效输入时,我在我的服务 class 中只得到“400 BAD REQUEST”结果,我称之为 API。但是当我使用 postman 分别调用 API 时,我得到了预期的输出。
代码示例:
class Abc {
ResponseEntity<String> = response;
static final String url = "https://abc-xyz.com/client-rest-end-point-url";
public ResponseEntity getDetails(RequestInput requestInput) {
try{
response=restTemplate.postForObject(url,requestInput,String.class);
} catch(Exception e) {
ResponseEntity response = (ResponseEntity<ErrorModel>)restTemplate.postForEntity(url,requestInput,ErrorModel.class);
}//try-catch
}//getDetails method
}//class
使用@ExceptionHandler 注释来注释您的方法。您可以从控制器中单独编码 class。
@ControllerAdvice
public class YourExceptionHandler {
@ExceptionHandler(CustomException.class)
public String xException() {
return "error/exception";
}
}
您可以为整个应用程序创建自定义异常 class,并且可以使用 throw
关键字在 JSON
中发送数据
假设你有例外 class 是:
public class TestException extends Exception {
private static final long serialVersionUID = 1L;
private String code;
private String detailMessage;
public TestException() {
};
public TestException(String message, String code, String detailMessage) {
super(message);
this.code = code;
this.detailMessage = detailMessage;
}
public TestException(String message, String code) {
super(message);
this.code = code;
}
//TestExceptionResponseCode is another class for message data, if required.
public TestException(TestExceptionResponseCode testExceptionResponseCode) {
super(testExceptionResponseCode.getMessage());
this.code = testExceptionResponseCode.getCode();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDetailMessage() {
return detailMessage;
}
public void setDetailMessage(String detailMessage) {
this.detailMessage = detailMessage;
}
}
现在在你的情况下抛出异常可以是这样的:
class Abc {
ResponseEntity<String> = response;
static final String url = "https://abc-xyz.com/client-rest-end-point-url";
public ResponseEntity getDetails(RequestInput requestInput) {
if(requestInput==null){
throw new TestException("FAILED", "1", "Data can't be null");
}
}