実現したいこと
java.lang.ClassCastExceptionを解決したい
発生している問題・エラーメッセージ
hrefで送信したリクエストパラメータをControllerで受け取り、その値を条件にSQLを実行しているのですが、途中で下記のエラーが発生します。
DBはmySQL、DB操作にはmybatisを使用しています。
java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
因みに、@GetMapping("/order")に入ったときは問題なく表示されるので、
orderForm.setProductList(productService.findProduct(categoryCd));
上記以降の処理に問題があると思われます。
初学者のため、読みづらいコードや、そもそも書き方間違ってるような箇所があったらすみません。
どんなことでもご教示いただけますと幸いです。
該当のソースコード
html
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="UTF-8"> 5<link rel="stylesheet" media="all" th:href="@{/css/style.css}"/> 6<title>商品ページ</title> 7</head> 8<body> 9 10<h3>商品ページ</h3> 11 12 13<article> 14<div class="side"> 15 <div class="row form-horizontal"> 16 <div class="col-sm-12"> 17 <h4 class="clear hidden-sm hidden-xs">商品分類</h4> 18 <div class="bs-component"> 19 <div class="list-group"> 20 <div th:each="productCategoryS : ${orderForm.getCategoryList}"> 21 <a 22 23 th:href="@{/order/product(categoryCd=${productCategoryS.getCategoryCd()})}" 24 class="list-group-item list-group-item-warning" 25 th:text="${productCategoryS.getName()}" 26 th:name="categoryCd"> 27 </a> 28 29 </div> 30 </div> 31 </div> 32 </div> 33 </div> 34</div> 35 36<div class="content"> 37<!-- 一覧 --> 38 39<table> 40 <thead> 41 <tr> 42 <th>製品名</th> 43 <th>数量</th> 44 </tr> 45 </thead> 46 <tbody> 47 <tr th:each="productList : ${orderForm.getProductList}" > 48 <td th:text="productList.getName" /></td> 49 <td th:text="productList.getQuantity" /></td> 50 </tr> 51 </tbody> 52 </table> 53 54</div> 55</article> 56<!-- main-content --> 57</body> 58</html>
entity
java
1 2import lombok.Data; 3 4@Data 5public class product { 6 7 private final String name; 8 private final String categoryName; 9 private final String quantity; 10}
form
java
1 2import java.util.List; 3 4import com.example.jrOrder.entity.category; 5import com.example.jrOrder.entity.product; 6 7import lombok.Data; 8 9@Data 10public class orderForm { 11 private int CategoryCd; 12 private List<category> categoryList; 13 private List<product> productList; 14}
controller
java
1package com.example.jrOrder.controller; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Controller; 5import org.springframework.ui.Model; 6import org.springframework.web.bind.annotation.GetMapping; 7import org.springframework.web.bind.annotation.PostMapping; 8import org.springframework.web.bind.annotation.RequestMapping; 9import org.springframework.web.bind.annotation.RequestParam; 10 11import com.example.jrOrder.form.loginForm; 12import com.example.jrOrder.form.orderForm; 13import com.example.jrOrder.service.*; 14 15 16@Controller 17public class loginController { 18 19 @Autowired 20 private productService productService; 21 22 @GetMapping("/login") 23 public String loginController() { 24 return "login"; 25 } 26 27 @GetMapping(value="/login",params="error") 28 public String error() { 29 return "login"; 30 } 31 32 @GetMapping("/order") 33 public String orderController(orderForm orderForm, Model model) { 34 35 orderForm.setCategoryList(productService.getCategoryList()); 36 model.addAttribute("orderForm", orderForm); 37 38 return "order"; 39 } 40 41 42//この処理を行ったときにエラー 43 @GetMapping("/order/product") 44 public String productController(orderForm orderForm, Model model, @RequestParam(name="categoryCd") int categoryCd){ 45 46 orderForm.setProductList(productService.findProduct(categoryCd)); 47 orderForm.setCategoryList(productService.getCategoryList()); 48 model.addAttribute("orderForm", orderForm); 49 50 return "order"; 51 } 52}
Service
java
1package com.example.jrOrder.service; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.context.annotation.Lazy; 7import org.springframework.security.core.userdetails.UsernameNotFoundException; 8import org.springframework.stereotype.Service; 9 10import com.example.jrOrder.entity.category; 11import com.example.jrOrder.entity.product; 12import com.example.jrOrder.repository.productRepository; 13 14@Service 15public class productService { 16 17 @Autowired 18 private productRepository productRepository; 19 20 public List<product> findProduct(int categoryCd){ 21 return productRepository.findProduct(categoryCd); 22 } 23 24 public List<category> getCategoryList(){ 25 return productRepository.getCategoryList(); 26 } 27}
Repository
java
1package com.example.jrOrder.repository; 2 3import java.util.List; 4 5import org.apache.ibatis.annotations.Mapper; 6 7import com.example.jrOrder.entity.category; 8import com.example.jrOrder.entity.product; 9 10@Mapper 11public interface productRepository { 12 13 public List<product> findProduct(int categoryCd); 14 15 public List<category> getCategoryList(); 16 17}
sql
xml
1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE mapper PUBLIC 3 "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5<mapper namespace="com.example.jrOrder.repository.productRepository"> 6 7 <select id ="findProduct" parameterType="java.lang.String" resultMap="productDetails"> 8 9 SELECT PRODUCTS.NAME, PRODUCTS.QUANTITY, CATEGORIES.NAME 10 FROM PRODUCTS 11 INNER JOIN CATEGORIES 12 ON PRODUCTS.CATEGORY = CATEGORIES.ID 13 WHERE PRODUCTS.CATEGORY = #{categoryCd} 14 15 16 </select> 17 18 <resultMap type="com.example.jrOrder.entity.product" id="productDetails"> 19 20 <result property="name" column="NAME"/> 21 <result property="category" column="CATEGORIES.NAME"/> 22 <result property="quantity" column="QUANTITY"/> 23 24 </resultMap> 25 26 27 28 <select id ="getCategoryList" resultMap="categoryDetails" > 29 30 SELECT ID, NAME 31 FROM CATEGORIES 32 33 </select> 34 35 <resultMap type="com.example.jrOrder.entity.category" id="categoryDetails"> 36 37 <result property="categoryCd" column="ID"/> 38 <result property="name" column="NAME"/> 39 40 </resultMap> 41 42 43</mapper>
Productテーブル
sql
1CREATE TABLE `products` ( 2 `id` int NOT NULL, 3 `name` varchar(255) NOT NULL, 4 `category` int NOT NULL, 5 `quantity` varchar(45) NOT NULL, 6 PRIMARY KEY (`id`), 7 UNIQUE KEY `id_UNIQUE` (`id`)
Categoryテーブル
sql
1CREATE TABLE `categories` ( 2 `id` int NOT NULL, 3 `name` varchar(255) NOT NULL, 4 PRIMARY KEY (`id`), 5 UNIQUE KEY `id_UNIQUE` (`id`)
試したこと
categoryCdをStringで受けとってからintに変換する処理をはさんで試してみましたが、同様のエラーでした。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

回答1件
あなたの回答
tips
プレビュー