java.lang.IllegalArgumentException: No converter found for return value of type 异常

这个的错误主要的意思就在于json的转换问题。

我看了之前的一些问题报错,一般就在于以下几个方面:

  1. pojo的javaBean的getBean取不到。
  2. Spring和json的包冲突。
  3. json的几个包没有导入。

很可惜我的问题都不是以上的问题。
这是我的表现层。

@Controller
public class ItemController {
 /*
 这两个都是关于取到json数据的 第一个就是从url中取出json数据

名字一致的话就不用加@PathVariable  名字不一致的话就需要加上Value属性
在这里主要就是加这个属性就是获取Json数据
  */
   @Autowired
  private ItemService itemService;

  @RequestMapping(value="/item/{itemId}")
   @ResponseBody
    public TbItem getItemById(@PathVariable Long itemId) throws Exception {
    TbItem tbItem = itemService.getItemId(itemId);
    System.out.println("测试用例"+tbItem.toString()+"--"+tbItem.getId()+"--"+tbItem.getTitle());
    return  tbItem;

   }

之后我就debug一下。我发现就是我的tbItem是能够取到值的。

在这里插入图片描述
所以这时候就要找到SpringMvc.xml的配置问题了。几经查找,发现就是这的问题。
适配器和映射器我写的代码如下:

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> </bean>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

我不知道我这个东西为啥写了两遍,这个注释掉就没有问题了。

为啥我会有这个错误,是因为我之前在SSM整合的时候,SpringMvc.xml没有报错,所以我才一直在使用。
总结一下,基本上这个错误上面的三种方式都能解决,实在不行debug,或者自己进行测试类,最后就是配置的问题,一定要理解这Spring适配器,映射器,注解驱动,试图解析器的意思。不要擅自更改,不一定这个项目正确,另一个就也正确。

猜你喜欢

转载自blog.csdn.net/weixin_37647123/article/details/86738459