java.lang.NoSuchMethodException

本文解决了一个关于MyBatis框架中实体类构造时出现的类型转换错误问题,通过正确配置typeHandler和javaType属性,成功实现了从数据库查询到实体类的映射。

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating class net.51zhiyuan.dev.pedicure.ui.model.Servicewithinvalidtypes(Integer,Integer,Integer,String,String,String,BigDecimal,BigDecimal,Integer,Integer,String,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Object,Boolean,Date,Date)orvalues(1,1,0,,DE,a8ee8d942f1848ed8d129b09f9c2a393,999.00,99.90,70,118,asdadsqeqe,1,0,1,0,1,1,1,[,,],false,ThuJun1415:06:30CST2018,SatSep0817:02:35CST2018).Cause:java.lang.NoSuchMethodException:net.51zhiyuan.dev.pedicure.ui.model.Servicewithinvalidtypes(Integer,Integer,Integer,String,String,String,BigDecimal,BigDecimal,Integer,Integer,String,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Object,Boolean,Date,Date)orvalues(1,1,0,第一,DE,a8ee8d942f1848ed8d129b09f9c2a393,999.00,99.90,70,118,asdadsqeqe,1,0,1,0,1,1,1,[热门服务,客户推荐,特价体验],false,ThuJun1415:06:30CST2018,SatSep0817:02:35CST2018).Cause:java.lang.NoSuchMethodException:net.51zhiyuan.dev.pedicure.ui.model.Service.(java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.math.BigDecimal, java.math.BigDecimal, java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Object, java.lang.Boolean, java.util.Date, java.util.Date)

在数据库中有这样一组数据
[“客户推荐”,“特价狂欢”]
我在实体类中定义的类型是List ,因此需要在mybatis解析的时候进行类型对应,开始的时候我只写了typeHandler
这里写图片描述
但是还是现实类型转不正确,所以又加了javaType的属性
这里写图片描述
现在问题解决了
ps:这里写图片描述

### java.lang.NoSuchMethodExceptionjava.lang.annotation.Target 初始化问题的原因及解决方案 java.lang.NoSuchMethodExceptionJava 中的一个异常,表示在类中找不到指定的方法。此异常通常发生在反射操作中,例如通过 `Class.getMethod()` 或 `Class.getDeclaredMethod()` 方法查找方法时[^1]。 #### 原因分析 1. **方法不存在**:如果指定的方法名或参数类型与实际类定义不符,则会抛出 `NoSuchMethodException`。这可能是因为方法确实不存在,或者方法签名(包括参数类型)不匹配。 2. **访问权限限制**:即使方法存在,但如果它是私有的或受保护的,并且调用者没有适当的访问权限,也会抛出此异常。 3. **初始化问题**:对于注解相关的类(如 `java.lang.annotation.Target`),如果注解未正确初始化或使用了错误的枚举值,可能会间接导致此类异常。例如,`Target` 注解需要一个 `RetentionPolicy` 枚举值作为参数[^3]。 ```java @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value(); } ``` #### 解决方案 1. **检查方法签名**:确保调用 `getMethod()` 或 `getDeclaredMethod()` 时提供的方法名和参数类型完全匹配目标类中的方法定义。例如: ```java Class<?> clazz = MethodTrouble.class; Method method = clazz.getMethod("find", Integer.class); // 正确的参数类型 ``` 2. **处理访问权限**:如果方法是私有的或受保护的,可以改用 `getDeclaredMethod()` 并设置可访问性: ```java Method method = clazz.getDeclaredMethod("find", Object.class); method.setAccessible(true); // 强制访问私有方法 ``` 3. **注解初始化问题**:对于 `java.lang.annotation.Target`,确保注解的 `ElementType` 枚举值正确无误。如果自定义注解未正确初始化,可能导致后续反射操作失败。例如: ```java @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.CLASS) public @interface CustomAnnotation { String value() default ""; } ``` 4. **编译警告处理**:当使用反射时,可能会出现未经检查的转换警告。可以通过显式指定泛型类型来避免警告。例如: ```java Method method = clazz.getMethod("m", new Class<?>[0]); ``` 5. **调试与日志**:在开发过程中,添加详细的日志记录以捕获异常堆栈信息,便于定位问题来源。 #### 示例代码 以下是一个完整的示例,展示如何正确处理 `NoSuchMethodException`: ```java import java.lang.reflect.Method; public class ReflectionExample { public static void main(String[] args) { try { Class<?> clazz = MethodTrouble.class; Method method = clazz.getMethod("find", Integer.class); // 确保参数类型匹配 System.out.println("Method found: " + method.getName()); } catch (NoSuchMethodException e) { System.err.println("Method not found: " + e.getMessage()); } } } class MethodTrouble { public void find(Integer value) { System.out.println("Value: " + value); } } ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值