错误信息
01-May-2020 20:43:35.866 WARNING [http-nio-555-exec-1] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object ‘books’ on field ‘bookId’: rejected value [null]; codes [typeMismatch.books.bookId,typeMismatch.bookId,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [books.bookId,bookId]; arguments []; default message [bookId]]; default message [Failed to convert value of type ‘null’ to required type ‘int’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [int] for value ‘null’; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type]]
解决方法
底层的books的参数重写为bookID
PS: 但是当我重写回bookId的时候又没有问题了,怀疑是idea有bug
下面是Books类的代码,一开始bookId报错,重写成bookID后不报错了,但是再次重写成bookId不报错.
package com.psj.pojo;
public class Books {
private int bookId;
private String bookName;
private int bookCounts;
private String detail;
public Books() {
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookCounts() {
return bookCounts;
}
public void setBookCounts(int bookCounts) {
this.bookCounts = bookCounts;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Books(int bookId, String bookName, int bookCounts, String detail) {
this.bookId = bookId;
this.bookName = bookName;
this.bookCounts = bookCounts;
this.detail = detail;
}
@Override
public String toString() {
return "Books{" +
"bookId=" + bookId +
", bookName='" + bookName + '\'' +
", bookCounts=" + bookCounts +
", detail='" + detail + '\'' +
'}';
}
}
疑问
虽然现在没有报错了,但是还是找不出原因