Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.List‘;

导读:本篇文章讲解 Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.List‘;,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Failed to convert value of type ‘java.lang.String’ to required type ‘java.util.List’; nested exception is java.lang.IllegalStateException: Cannot convert value of type ‘java.lang.String’ to required type ‘com.lanxi.hm.model.DrugParam’: no matching editors or conversion strategy found

today at 14:17:30   2022-12-08 14:17:30.837, [http-nio-7065-exec-2], ERROR, com.lanxi.hm.advice.DetailedControllerAdvice, 39, [detailedExceptionHandler] url:http://hm/hm/medicineBox/setAlarm. 
today at 14:17:30   org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'java.util.List'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.lanxi.hm.model.DrugParam': no matching editors or conversion strategy found
today at 14:17:30	at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:129)
today at 14:17:30	at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
today at 14:17:30	at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
today at 14:17:30	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
today at 14:17:30	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
today at 14:17:30	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
today at 14:17:30	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
today at 14:17:30	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
today at 14:17:30	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
today at 14:17:30	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
today at 14:17:30	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
today at 14:17:30	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
today at 14:17:30	at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
today at 14:17:30	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
today at 14:17:30	at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
...
...

我现在是写接口,对接第三方接口,第三方接口文档如下
在这里插入图片描述
此时,我写的接口如下:
在这里插入图片描述
在这里插入图片描述
看起来并没有什么问题,但是却报上文展示的错误

无法将类型“java.lang.String”的值转换为所需类型“java.util.List”;
嵌套异常为java.lang.IllegalStateException:
无法将“java.lang.String”类型的值转换为所需类型“com.lanxi.hm.model.DrugParam”:
找不到匹配的编辑器或转换策略

既然它传参传进来的是String,那我为什么非要把他转换成List<DrugsParam>
于是,我将接口参数类型直接就写成了String,修改后的接口如下:

@ApiOperation(value = "编辑闹钟接口")
@PostMapping("/setAlarm")
public Rs<String> setAlarm(
	@ApiParam(value = "token,通过‘获取token’接口获取") @RequestParam(value = "token", required = true) String token,
    @ApiParam(value = "用户ID") @RequestParam(value = "userId", required = true) String userId,
    @ApiParam(value = "设备编码") @RequestParam(value = "deviceSn", required = true) String deviceSn,
    @ApiParam(value = "闹钟ID") @RequestParam(value = "alarmId", required = true) String alarmId,
    @ApiParam(value = "闹钟时间,例08:05") @RequestParam(value = "alarmTime", required = true) String alarmTime,
    @ApiParam(value = "闹钟状态,1有效,2无效") @RequestParam(value = "status", required = true) String status,
    @ApiParam(value = "药品信息(数组),例如:[{ \"drug_name\":\"drug 1\", \"drug_amount\":\"1\" }, {\"drug_name\":\"drug 2\",\"drug_amount\":\"2\"}]") @RequestParam(value = "drugs", required = false) String drugs) {
    return Rs.success(medicineBoxService.setAlarm(token, userId, deviceSn, alarmId, alarmTime, status, drugs));
    }

关键的地方在这里
在这里插入图片描述
然后,居然报了空指针
在这里插入图片描述
代码在这里
在这里插入图片描述
接口在这里
在这里插入图片描述
细想,确实是会报空指针,于是一顿操作猛如虎,修改!加个判断不就可以了么~

在这里插入图片描述
ok,解决了
在这里插入图片描述
但是当我传参的时候
在这里插入图片描述
又一次报错~哦,我傻了,数组怎么转换成Json呢,直接传字符串不就好了,马大哈吗我是。。。
在这里插入图片描述
修改后的代码
在这里插入图片描述
ok彻底解决了

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/65542.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!