Search code examples
javaconstraintsgoogle-cloud-endpointsgoogle-cloud-endpoints-v2

How to handle fields' values constraints on Google Cloud Endpoints and Java?


I come from Spring boot (with Jersey) to make APIs REST where I could easily handle if I wanted some value from a field have any constraint, like having some maximum value or not being null with the library javax.validation.

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

...

@PUT
@Produces(MediaType.APPLICATION_JSON)
public Response gestionarVenta(@Valid @NotNull(message = CONSTRAINT_BODY_NOT_NULL) final ProductoCompradoDTO productoCompradoDTO) {
   ...
}

However, I'm checking out Google Cloud Endpoints now and I tried to use this same library but it just ignores it and goes on into the method.

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Api(name = "echo", version = "v1", namespace = @ApiNamespace(ownerDomain = "echo.example.com", ownerName = "echo.example.com", packagePath = "")
// ...
)
public class MyApi {
    @ApiMethod(name = "echo")
    public Message echo(@Valid @NotNull(message = "cant be null") Message message, @Named("n") @Nullable Integer n) {
        ...
    }
}

I'm sending an empty body.

Why is it ignoring it? Is there some way or library to put constraints to fields' values on Google Cloud Endpoints?

Thanks!


Solution

  • Performing checks expressed by javax.validation annotations on REST API calls is a feature implemented in Spring Boot framework.

    As of October 2019, Cloud Endpoints Frameworks for Java doesn't implement javax.validation checks. Also, it doesn't support equivalent features. You can file a feature request on https://github.com/cloudendpoints/endpoints-java/issues