要求属性在模型中具有值,不允许为null

时间:2018-10-31 16:33:18

标签: java spring spring-mvc spring-data-jpa

我似乎无法找到以下情况的解决方案。我在两者之间都有一个控制器,一个模型和一个服务,以验证POST请求中的数据。

当不满足约束条件时,nameage属性会返回错误,而hungry属性则没有给出错误,该属性应始终具有值。我将其设置为@NotNull@NotEmpty,但是当hungry请求中的POST属性没有值时,它们都没有给出我想要的结果。我是用错误的方式来处理这个问题吗?

控制器

package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
public class IndexController {

    private UserService userService;

    public IndexController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping
    ResponseEntity create(@Valid User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseEntity.badRequest().body(bindingResult.getAllErrors());
        }
        return userService.create(user);
    }
}

模型

package com.example.demo.model;

import com.google.gson.annotations.Expose;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.*;

@Entity
public class User {

    @Id
    @GeneratedValue
    private int id;

    @Size(min = 2, max = 15)
    @Expose
    private String name;

    @Min(18)
    @Max(100)
    @Expose
    private int age;

    @Expose
    private boolean hungry;


    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isHungry() {
        return hungry;
    }

    public void setHungry(boolean hungry) {
        this.hungry = hungry;
    }
}

服务

package com.example.demo.service;

import com.example.demo.model.User;
import com.example.demo.repo.UserRepository;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionSystemException;

import javax.persistence.RollbackException;
import javax.validation.ConstraintViolationException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.concurrent.ExecutionException;

@Service
public class UserService {

    private UserRepository userRepository;


    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;

        //User newU = new User("Isaak", 30);
        //this.userRepository.save(newU);
    }

    public ResponseEntity create(User user) {
        userRepository.save(user);
        return ResponseEntity.ok().build();
    }
}

我的要求是:

POST
http://localhost:8080/
name=1
age=250

响应:

[{"codes":["Size.user.name","Size.name","Size.java.lang.String","Size"],"arguments":[{"codes":["user.name","name"],"arguments":null,"defaultMessage":"name","code":"name"},15,2],"defaultMessage":"moet tussen 2 en 15 liggen","objectName":"user","field":"name","rejectedValue":"j","bindingFailure":false,"code":"Size"},{"codes":["Max.user.age","Max.age","Max.int","Max"],"arguments":[{"codes":["user.age","age"],"arguments":null,"defaultMessage":"age","code":"age"},100],"defaultMessage":"moet kleiner of gelijk aan 100 zijn","objectName":"user","field":"age","rejectedValue":250,"bindingFailure":false,"code":"Max"}]

2 个答案:

答案 0 :(得分:1)

原始类型不能为null,但可以使用带有Boolean注释的@NotNull

答案 1 :(得分:1)

您不能将Java原语分配为null,不能将int更改为Integer,将布尔值更改为Boolean。

@Entity
public class User {

    @Id
    @GeneratedValue
    private int id;

    @NotNull
    @Size(min = 2, max = 15)
    private String name;

    @NotNull
    private Integer age;

    @NotNull
    private Boolean hungry;
}