SpringMVC返回JSON提示:HttpMessageNotWritableException: No converter found for return value of type

版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/sinat_34104446/article/details/82771341

目录

一、问题描述

二、问题分析

三、解决步骤


一、问题描述

1.今天使用SpringMVC+@ResponseBody返回JSON数据格式的时候发现提示下面的错误

2.错误内容为:Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class xxx.Xxx

3.出现错误以后上网找答案发现根本没有自己想要的,就算有看半天也没有说怎么解决,所以特记下这个问题给遇到同样问题的朋友参考

二、问题分析

1.首先看一下控制器,返回String类型正常,但是当返回对象的时候就出异常提示:No converter found for return value of type

2.出现这个问题的根本原因就是在SpringMvc配置文件中没有启动注解扫描!也就是没有开启<mvc:annotation-driven />,那为什么开启这个标记呢?原因就是<mvc:annotation-driven /> 会自动注册以下两个bean:

  • DefaultAnnotationHandlerMapping
  • AnnotationMethodHandlerAdapter 

这两个bean是spring MVC为@Controllers分发请求所必须并提供了数据绑定支持,如

  • @NumberFormatannotation
  • @DateTimeFormat
  • @Valid
  • XML读写(JAXB)
  • JSON读写(Jackson)

3.知道原因之后只需在配置文件中开启注解扫描就行了

三、解决步骤

1.开启注解扫描

2.重新访问,JSON数据成功显示 {"id":1,"name":"SpringMVC","age":22}

3.需要注意,必须导入相关jar包,如

4.SpringMVC配置文件完整配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<context:component-scan base-package="com.codecoord"/>
	
	<!-- 配置视图解析器 -->
	<bean id="viewResolver" 
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"/>
		<property name="suffix" value=".jsp"/>
	</bean> 
	
	<!-- 开启注解扫描 -->
	<mvc:annotation-driven/>
</beans>

5.额外提示:如果使用Maven,则只需要加入以下两个依赖

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.codecoord</groupId>
	<artifactId>SpringMVC</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<!-- 加载SpringMVC依赖 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.0.9.RELEASE</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
		    <groupId>com.fasterxml.jackson.core</groupId>
		    <artifactId>jackson-databind</artifactId>
		    <version>2.9.1</version>
		</dependency>
	</dependencies>
</project>

猜你喜欢

转载自blog.csdn.net/sinat_34104446/article/details/82771341