Reputation: 1652
I have a rest service and if I have a method for an example POST which consume application/json and the argument of the method is another class which describe the fields of the require json is there a way to make jackson to validate the input json before enter the method? Here is a little code :
@PUT
@Consumes("application/json")
public UserDataResourceObject login(UserResourceObject userResourceObject)
{
Query query = getEm().createNamedQuery("User.authorize");
if (userResourceObject == null)
{
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}
query.setParameter("username", userResourceObject.getUsername());
query.setParameter("password", Utility.getMD5Value(userResourceObject.getPassword()));
Exception e = null;
try
.................
And the UserResourceObject describe this structure :
{
"username" : "admin",
"password" : "123456"
}
I want if I change the structure of the json above jackson to throw me an exception for an example 400 (Bad request). How can I do that?
Upvotes: 0
Views: 3631
Reputation: 938
You can use Bean Validation (JSR 303), to validate the data you receive. You have http://bval.apache.org/ and http://hibernate.org/validator/ implementations.
Then you can do it that way:
@PUT
@Consumes("application/json")
public UserDataResourceObject login(@Valid UserResourceObject userResourceObject) {
Upvotes: 4