使用 Spring Data JPA 获取 NumberFormatException

Dis*_*lue 2 java spring jpa numberformatexception

我正在通过“Spring in action 5”学习 Spring 并遇到一些问题:当我在第 3 章中从 JDBC 切换到 Spring Data JPA(100% 正常工作)时,代码停止工作,因为我尝试打开主页塔可的成分。我做了一些日志来查看发生了什么,发现方法 findById(String id) 无法从 DB(或类似的东西)转换我的值。我正在使用 MySQL。

我尝试使用@Autowired 自己调用转换器的方法 convert(String id),但我发现的唯一一件事是当键错误时,会出现另一个错误。所以数据是可见的。我会尝试在这里提供一些代码,但我不确定什么有用,什么没用。我在第一次尝试记录某些东西时出错。IDE 和浏览器中的错误是不同的。这是完整的项目https://github.com/thedistantblue/taco-cloud-jpa

这是我的转换器:

    public class IngredientByIdConverter implements Converter<String, 
    Ingredient> {

    private IngredientRepository ingredientRepo;

    @Autowired
    public IngredientByIdConverter(IngredientRepository ingredientRepo) {
        this.ingredientRepo = ingredientRepo;

    }
    @Override
        public Ingredient convert(String id) {
            log.info("In converter.convert(): " 
        +ingredientRepo.findById(id).toString());
            Optional<Ingredient> optionalIngredient = 
        ingredientRepo.findById(id);
            return optionalIngredient.orElse(null);
        }
    }
Run Code Online (Sandbox Code Playgroud)

和控制器类:

    @Slf4j
    @Controller
    @RequestMapping("/design")
    @SessionAttributes("order")
    public class DesignTacoController {

    @ModelAttribute(name = "order")
    public Order order() {
        return new Order();
    }

    @ModelAttribute(name = "taco")
    public Taco taco() {
        return new Taco();
    }

    private final IngredientRepository ingredientRepository;
    private TacoRepository designRepository;
    private IngredientByIdConverter converter;

    @Autowired
    public DesignTacoController(IngredientRepository ingredientRepository,
                                TacoRepository designRepository,
                                IngredientByIdConverter converter) {
        this.ingredientRepository = ingredientRepository;
        this.designRepository = designRepository;
        this.converter = converter;
    }

    @GetMapping
    public String showDesignForm(Model model) {
        List<Ingredient> ingredients = new ArrayList<>();



        log.info(converter.convert("CARN").getName());
        log.info("in DTC: " + ingredientRepository.findAll());

        ingredientRepository.findAll().forEach(i -> ingredients.add(i));
Run Code Online (Sandbox Code Playgroud)

在IDE中:

java.lang.NumberFormatException:对于输入字符串:“PROTEIN”在 java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054) ~[na:na] 在 java.base/jdk.internal.math .FloatingDecimal.parseDouble(FloatingDecimal.java:110) ~[na:na] 在 java.base/java.lang.Double.parseDouble(Double.java:543) ~[na:na]

在浏览器中:

出现意外错误(类型=内部服务器错误,状态=500)。对于输入字符串:"PROTEIN"; 嵌套异常是 java.lang.NumberFormatException:对于输入字符串:“PROTEIN” org.springframework.dao.InvalidDataAccessApiUsageException:对于输入字符串:“PROTEIN”;嵌套异常是 java.lang.NumberFormatException:对于输入字符串:org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:373) 处的“PROTEIN”在 org.springframework.orm.jpa.vendor.HibernateJpaDialect.transibleExceptionIfPoss HibernateJpaDialect.java:255) 在 org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527) 在 org.springframework.dao.support。

小智 8

对于 Ingredient.java 需要为字段添加注解 @Enumerated(EnumType.STRING) Type type

@Id
@NaturalId(mutable = false)
private final String id;
private final String name;
@Enumerated(EnumType.STRING)
private final Type type;

public static enum Type {
    WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
}
Run Code Online (Sandbox Code Playgroud)