前提:已搭建好环境

1.建立Controller

 package com.ice.controller;

 import com.ice.model.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @RequestMapping("/person")
@Controller
public class PersonController {
@RequestMapping("/get")
@ResponseBody
public Person get(){
Person person=new Person();
person.setAge(18);
person.setName("ice");
return person;
}
}

访问后报错,如下

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

 <mvc:annotation-driven>
<mvc:message-converters>
<!--返回普通字符串-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

3.重新运行ok

{"age":18,"name":"ice"}

spring入门(四) spring mvc返回json结果的更多相关文章

  1. spring mvc返回json字符串的方式

    spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json            优点:不需要自己再处理 步骤一:在spring- ...

  2. spring mvc返回json字符串数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable

    1.spring mvc返回json数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable 2. @RequestMapping(val ...

  3. 转:spring mvc返回json数据格式

    转:http://www.cnblogs.com/ssslinppp/p/4675495.html <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: ...

  4. spring mvc 返回json数据的四种方式

    一.返回ModelAndView,其中包含map集 /* * 返回ModelAndView类型的结果 * 检查用户名的合法性,如果用户已经存在,返回false,否则返回true(返回json数据,格式 ...

  5. Spring MVC返回json数据给Android端

    原先做Android项目时,服务端接口一直是别人写的,自己拿来调用一下,但下个项目,接口也要自己搞定了,我想用Spring MVC框架来提供接口,这两天便抽空浅学了一下该框架以及该框架如何返回json ...

  6. 使用spring mvc返回JSON,chrome可以,firefox不行的问题定位

    转载http://ks.netease.com/blog?id=4024 作者:李景     场景:          前端Post请求同一个url地址,在chrome浏览器上有正常返回json,而在 ...

  7. spring mvc 返回json的配置

    转载自:http://my.oschina.net/haopeng/blog/324934 springMVC-servlet.xml 配置 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  8. Spring MVC返回json格式

    在使用SpringMVC框架直接返回json数据给client时,不同的版本号有差异. 以下介绍两种类型的版本号怎样配置. 注意:这两种方法均已验证通过. 1.Spring3.1.x版本号 1.1 d ...

  9. Spring MVC 返回Json IE出现下载

    今天在做一个利用IFrame提交进行form提交表单的时候发现返回的json在ie下竟然弹出了下载的提示, 于是就查看了返回的Content-type:appliation/json;charset= ...

随机推荐

  1. python学习(四)--POST请求

    from urllib import requestimport urllibimport re #网页版有道翻译是ajax的post请求. #浏览器请求地址http://fanyi.youdao.c ...

  2. springboot如何实现微信登录,前期准备

    现在网站用微信登录真的是很多,那么具体是怎么实现的呢? 首先介绍的是微信开放平台,我们如果需要微信登录或者支付都需要在上面注册一个账号,用这个账号去为我们的网站申请的话,需要用到企业资料(家里有营业执 ...

  3. 译:面试投行的20个Java问题

    原文链接:https://dzone.com/articles/var-work-in-progress 作者:Anghel Leonard 译者:沈歌 如果你需要准备面试,可以看一下这篇博客中20个 ...

  4. scss-变量作用域

    SCSS之所以便利,是因为它具有了编程语言的某些特性. 让原本规则刻板的CSS变的灵活起来,下面介绍一下SCSS中的作用域概念. 几乎所有编程语言都有作用域概念的涉及,原理大同小异,SCSS中的也是如 ...

  5. 转 【<meta name="description" content=">】作用讲解

    今天在看别人写的网站代码,发现类似<meta name="Keywords" content="" >.<meta name="De ...

  6. JVM Guide

    Java Virtual Machine: the Essential Guide October 8th, 2014 - By Alexey Zhebel Introduction Java Vir ...

  7. .NET开源工作流RoadFlow-表单设计-附件管理

    在表单中添加一个附件管理的控件: 文件类型:指定可以上传的文件类型.

  8. B树索引学习

    https://blog.csdn.net/wl044090432/article/details/53423333

  9. 【起航计划 035】2015 起航计划 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller

    Local Service Controller 是将LocalService当作“Started”Service来使用,相对于”Bound” Service 来说,这种模式用法要简单得多,Local ...

  10. 一、选择与安装——Linux学习笔记

    A)为什么要用Linux做服务器? 网络理由: 1.linux本身是网络操作系统,支持所有TCP/IP协议,网络功能是内核中六大模块之一 2.linux和unix兼容,unix是早期的服务器霸主,现在 ...