【问题标题】:Handling invalid http request parameters API Restful处理无效的http请求参数API Restful
【发布时间】:2020-06-23 00:37:48
【问题描述】:

我创建了一个通过 http 请求进行通信的程序。我使用 Postman 发送请求。当我进行注册时,我调用 API 方法。如果此方法抛出一些异常,我会使用“HandlerMapping”来管理它,它会捕获异常并发送关于它的个性化消息。

HandlerMapping 类:

@Provider
public class HandlerMapper implements ExceptionMapper<InputValidationException>{

    @Override
    public Response toResponse(InputValidationException exception) {
        return Response.status(exception.getStatus().getStatusCode(), exception.getMessage()).build();
    }
}

InputValidationException:

public class InputValidationException extends Exception{
    private String errorMessage;
    private Response.Status status;

    @JsonbCreator
    public InputValidationException (@JsonbProperty("message") String message, @JsonbProperty("status") Response.Status status) {
        this.errorMessage = "Invalid param entered: " + message;
        this.status = status;
    }
.........
}

现在,当它从 API 方法中抛出异常时,它可以正常工作,并按照我的意愿发送客户消息。但是,如果我发送带有错误参数的消息(例如名称为 null),则不会像使用 api 方法那样创建自定义响应错误,而是使用 500 服务器错误创建默认消息。如何制作通用类以个性化的方式处理错误?

客户客户

public class Client {
    private String surname;
    private String name;
    private String city_of_birth;
    .... another parameters ....
        
    @JsonbCreator
    public Cliente(@JsonbProperty("surname") String surname, @JsonbProperty("name") String name, @JsonbProperty("city_of_birth") String city_of_birth) throws InputValidationException {
        paramValidation(surname, name, city_of_birth, ......... );
        this.surname= surname;
        this.name= name;
        this.city_of_birth= city_of_birth;
        .... another parameters ....     
    }

    private void paramValidation(String surname, String name, String city_of_birth) throws InputValidationException{
        if( surname == null || surname.isBlank() ){
            throw new InputValidationException("surname", Response.Status.METHOD_NOT_ALLOWED);
        }
        .... other parameter controls ....
    }

}

API 类

@Path("homeBanking/client/signup")
public class Registration {
    private DaoClient daoC = new DaoClient();

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response createClient(Client client) throws InputValidationException {
        daoC.insert(client);
        return Response.ok().build();
    }
}

【问题讨论】:

    标签: java api rest httprequest httpresponse


    【解决方案1】:

    我认为在您的上下文中,解决方案是创建一个 Exception Mapper 以将异常消息转换为 JSON 对象,遵循以下结构:https://docs.jboss.org/resteasy/docs/3.0.6.Final/userguide/html/ExceptionHandling.html

    使用Spring 框架,您可以使用ControllerAdvice 创建您自己的错误消息。见这篇文章:https://www.baeldung.com/exception-handling-for-rest-with-spring

    希望对你有所帮助!

    【讨论】:

    • 首先感谢您的回复。如您所见,我已经使用了 ** Exception Mapper ** 来转换异常。另外,我已经看过 Spring Framework,但我想使用不同的解决方案来解决我的问题。
    • 对不起。我没有正确地看到你的问题。再次阅读它,我认为解决方案可能是创建一个“超级”异常,所有异常都可以扩展。之后,仅为此异常创建一个映射器。也许在其中,它可能有一个状态错误。
    猜你喜欢
    • 1970-01-01
    • 2015-08-08
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 2013-01-30
    相关资源
    最近更新 更多