我似乎无法找到以下情况的解决方案。我在两者之间都有一个控制器,一个模型和一个服务,以验证POST请求中的数据。
当不满足约束条件时,name
和age
属性会返回错误,而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"}]
答案 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;
}