1

i trying to validate my Model class, which is named user.java, i trying to validate the column to prevent it not empty and the email is should be email format, and i use @RepositoryRestController to create my CRUD api, but how do i do this properly?

here is my user.java :

package com.mechadevapi.mechadev.Models;

import java.util.Date;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;

import org.springframework.data.annotation.Id;

public class User {

    @Id
    public String Id;

    @NotBlank(message = "is empty")
    public String username;
    @NotBlank(message = "is empty")
    @Email(message = "should be email")
    public String email;
    @NotBlank(message = "is empty")
    public String firstname;
    public String lastname;
    @NotBlank(message = "is empty")
    public String password;
    @NotBlank(message = "is empty")
    public String level;
    @NotBlank(message = "is empty")
    public Boolean activation;
    public Date entry_time;
    public Date last_login;

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public Boolean getActivation() {
        return activation;
    }

    public void setActivation(Boolean activation) {
        this.activation = activation;
    }

    public Date getEntry_time() {
        return entry_time;
    }

    public void setEntry_time() {
        Date entry_time = new Date();

        this.entry_time = entry_time;
    }

    public Date getLast_login() {
        return last_login;
    }

    public void setLast_login() {
        Date last_login = new Date();

        this.last_login = last_login;
    }



}

and here is my repository, UserRepository.java :

package com.mechadevapi.mechadev.Repository;

import javax.validation.Valid;

import com.mechadevapi.mechadev.Models.*;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "user", path = "user")
@Valid
public interface UserRepository extends MongoRepository<User, String> {

}

when i trying to post the data using postman, which is looked like this :

{
    "username" : "admin",
    "email" : 123,
    "firstname" : "ad",
    "lastname" : "min",
    "password" : "123456",
    "level" : "administrator",
    "activation" : true
}

the data still saved, it's like the javax.validation anotation is not working, how do i validate this properly?

Ke Vin
  • 3,478
  • 11
  • 60
  • 91
  • have you tried this https://mkyong.com/spring-boot/spring-rest-validation-example/ – bananas Jan 30 '20 at 03:56
  • I don't think that you can apply `@Valid` to an interface like that; try either a method parameter (re-declare the method) or `@Validated`. – chrylis -cautiouslyoptimistic- Jan 30 '20 at 03:57
  • @emotionlessbananas i didn't use RestController so i can't implement @Valid in my controller and i think the only way is using Custom Validator, but is there a way to may the javax.validation work? – Ke Vin Jan 30 '20 at 04:05
  • @chrylis-onstrike- can u give me an example of it? i kind of new in java and spring – Ke Vin Jan 30 '20 at 04:05
  • Ensuring, you were saving NEW user with "email" : 123", not updating the existing one, yes? – itwasntme Jan 30 '20 at 05:23
  • @itwasntme yep it save as new record, i am doing a user registration – Ke Vin Jan 30 '20 at 06:44
  • If you don't want to use a controller method to handle the request and validate the request body, you could try to declare a custom method in your repository interface like `public void saveUser(@Valid User newUser);`. This would require a specific query tho. I would recommend the usage of a `RestController`. – Melkor Jan 30 '20 at 09:30
  • @Melkor i think i find a way to modified the current endpoint that created with RepositoryRestResource, and create a controller with RepositoryRestController annotation, i will share here if i find the solution. – Ke Vin Jan 31 '20 at 03:31
  • well if you are updating records, then I will recomend using proper `rest verbs`, `put vs patch` not post to update. – bananas Jan 31 '20 at 07:03
  • Does this answer your question? [Spring-Data-Rest Validator](https://stackoverflow.com/questions/24318405/spring-data-rest-validator) – bananas Jan 31 '20 at 11:38

0 Answers0