简体   繁体   English

如何将API异常输出传递给自己的REST服务?

[英]How to pass API exception output to through own REST service?

Summary : I want to pass valid exception output given by one REST service end point to the end user by using my own Rest service. 摘要:我想通过使用我自己的Rest服务将一个REST服务端点给出的有效异常输出传递给最终用户。

What I did is, I have called that service in service class using RestTemplate class, it's giving valid output on valid post request. 我所做的是,我已经使用RestTemplate类在服务类中调用了该服务,它会在有效的发布请求后提供有效的输出。 But when I am passing invalid input to it I am getting only '400 BAD REQUEST' result in my service class where I have called that API. 但是,当我向其传递无效输入时,在我调用该API的服务类中仅得到“ 400错误请求”结果。 But when I am calling that API separately using postman, there I'm getting expected output. 但是,当我使用邮递员分别调用该API时,得到了预期的输出。

Code sample : 代码示例:

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

Annotate your method with @ExceptionHandler annotation. 使用@ExceptionHandler注释对方法进行注释。 You can code in seperate class from controller. 您可以在与控制器分开的类中进行编码。

@ControllerAdvice
public class YourExceptionHandler {

@ExceptionHandler(CustomException.class)
public String xException() {
 return "error/exception";
}
}

You can create a custom exception class for your entire application and you can send data in JSON by using throw keyword Suppose you have exception class is: 您可以为整个应用程序创建一个自定义异常类,并且可以使用throw关键字以JSON格式发送数据。假设您具有以下异常类:

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;
}

} }

Now in your case throwing exception can be like : 现在,在您的情况下,抛出异常可以像:

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");
    }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 Gateway/Api Gateway/Rest 将外部服务连接到队列? - How to connect a external service to a queue through Gateway / Api Gateway / Rest? 创建我自己的 REST API 会引发 servlet 执行异常 - Creating my own REST API throws servlet execution exception 如何在 ExpressionEvaluatingRequestHandlerAdvice 中捕获异常并抛出我自己的异常并通过我自己的异常处理程序进行处理? - How to catch exception and throw my own exception inside ExpressionEvaluatingRequestHandlerAdvice and handle it through my own exception handler? 如何将 InputStream 传递给 REST 服务 POST 方法 - How to pass InputStream to REST service POST method 如何将文件作为参数传递给POST REST服务 - How To Pass File as a Parameter to POST REST service REST服务的异常层次结构 - Exception hierarchy for a REST service 春季休息例外 - spring rest exception in service 如何在从服务引发异常的 rest controller 中模拟异常 - How to mock a exception in rest controller where the exception is thrown from a service Socialcast Rest Api服务的UTF-8输出 - UTF-8 output from Socialcast Rest Api service 如何通过WSO2(API管理器)将自定义授权标头传递到我的后端Websocket服务? - How to pass Custom Authorization Header to my backend websocket service through WSO2 (API Manager)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM