How to handle Resource validation in REST webservices?

前端 未结 4 1949
悲哀的现实
悲哀的现实 2020-12-24 14:46

I\'m building a REST webservice in Java using Spring, Jersey and Hibernate (JPA).

Now I\'m trying to add support for validation in my resources. JSR-303 (Bean valida

4条回答
  •  情歌与酒
    2020-12-24 15:41

    You can make use of jersey filters, where you can make some validations on your request data.

    Well, the category with id 123 might not exist. If we try to insert this into the database, obviously we get a foreign key related exception.

    For such cases, one can rely on the validations on data access level. But if you want to respond to your client synchronously, it is usually better to have this business logic exposed by some service API. i.e. You may have an API like isValidCategory(int id), which can be queried at a higher level than DAO so that you don't really have to wait till the data access layer encounters an error/exception. Obviously, this can still mean a database lookup (depends on your system), but optimizing that to improve your response time using Caching or some other mechanism is an entirely different problem altogether.

    Talking about bean validators, you can take a look at Hibernate Validator which is a JSR 303 implementation.

提交回复
热议问题