spring入门(四) spring mvc返回json结果
程序员文章站
2022-07-10 13:50:19
前提:已搭建好环境 1.建立Controller 访问后报错,如下 Type Exception ReportMessage No converter found for return value of type: class com.ice.model.PersonDescription The ......
前提:已搭建好环境
1.建立controller
1 package com.ice.controller; 2 3 import com.ice.model.person; 4 import org.springframework.stereotype.controller; 5 import org.springframework.web.bind.annotation.requestmapping; 6 import org.springframework.web.bind.annotation.responsebody; 7 8 @requestmapping("/person") 9 @controller 10 public class personcontroller { 11 @requestmapping("/get") 12 @responsebody 13 public person get(){ 14 person person=new person(); 15 person.setage(18); 16 person.setname("ice"); 17 return person; 18 } 19 }
访问后报错,如下
type exception report
message no converter found for return value of type: class com.ice.model.person
description the server encountered an unexpected condition that prevented it from fulfilling the request.
exception
org.springframework.http.converter.httpmessagenotwritableexception: no converter found for return value of type: class com.ice.model.person
org.springframework.web.servlet.mvc.method.annotation.abstractmessageconvertermethodprocessor.writewithmessageconverters(abstractmessageconvertermethodprocessor.java:226)
2.解决方法
引入依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version>1.2.47</version> </dependency>
修改spring-configure.xml
1 <mvc:annotation-driven> 2 <mvc:message-converters> 3 <!--返回普通字符串--> 4 <bean class="org.springframework.http.converter.stringhttpmessageconverter"/> 5 <bean class="com.alibaba.fastjson.support.spring.fastjsonhttpmessageconverter"> 6 <property name="supportedmediatypes"> 7 <list> 8 <value>text/html;charset=utf-8</value> 9 <value>application/json;charset=utf-8</value> 10 </list> 11 </property> 12 </bean> 13 </mvc:message-converters> 14 </mvc:annotation-driven>
3.重新运行ok
{"age":18,"name":"ice"}
上一篇: 史上最全 40 道 Dubbo 面试题及答案,看完碾压面试官!
下一篇: 等下把这页撕下来给你就是
推荐阅读
-
spring Mvc配置xml使ResponseBody返回Json的方法示例
-
Spring mvc实现Restful返回json格式数据实例详解
-
spring Mvc配置xml使ResponseBody返回Json的方法示例
-
Spring mvc实现Restful返回json格式数据实例详解
-
Android 客户端通过内置API(HttpClient) 访问 服务器(用Spring MVC 架构) 返回的json数据全过程
-
Spring 3 mvc中返回pdf,json,xml等不同的view
-
spring mvc捕获异常时,如何判断应该返回json还是跳转错误页面
-
spring入门(四) spring mvc返回json结果
-
解决spring mvc 返回json数据到ajax报错parseerror问题
-
spring mvc(四)返回json