有一些实体:
@Entity
@Table(name = "Peoples")
public class People implements Serializable {
@Id
@GeneratedValue(strategy = GeneratedType.AUTO)
private Long id;
private Profession profession;
стандартные геттеры, сеттеры
}
职业领域由一个枚举表示:
public enum Profession {
MANAGER,
COUNTER,
ENGINEER,
}
在数据库表中,Profession 字段存储为 VARCHAR。有几个条目:
| ID | 职业 |
|---|---|
| 0 | 经理 |
| 一 | 经理 |
| 2 | 柜台 |
在@RestController 中,我试图通过以下方式无条件返回表的所有记录:
@Autowired
private PageingAndSortingRepository<People, Long> peopleRepository;
@GetMapping
public List<People> getAllPeople() {
return peopleRepository.findAll(PageRequest.of(0, 3, Sort.by("id"))).toList();
}
我收到一个错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: For input string: "MANAGER"; nested exception is java.lang.NumberFormatException: For input string: "MANAGER"
数据必须以 JSON 格式在响应正文中发送:
{
"id":[Long]
"profession":[Profession]
}
我没有设置任何 setter 和 getter,我尝试用字符串值重写枚举,我尝试编写转换器,但没有任何结果。我无法理解这个错误的本质以及它的根源。如果有人能提供帮助,我将不胜感激。
使用@Enumerated(EnumType.STRING) 注释来标记枚举在数据库中的存储方式。