`
jander
  • 浏览: 9316 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Make Spring insist json in a easy way

阅读更多
在网上看了一些spring 支持json的方法,大致有两类:
一种是继承Controller.这种方法对于multi controller不方便。
一种是继承View.这种方法更麻烦,还需要对配置文件进行配置。
下面提供一个我一直用的,很简单方便。


/**
* Spring json callback class
*
* @Author jander.sy@163.com
*/
public abstract class JsonCallback {
	public void doInController(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("application/json;charset=utf-8");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
			String jsonString=null;
			try {
				jsonString=this.createJson(request);
			} catch (Exception e) {
				e.printStackTrace();
				StringBuffer sb=new StringBuffer("{'result':'error','exception':{'message':'");
				sb.append(e.getMessage());
				sb.append("','stackTrace':'");	
				for(StackTraceElement st:e.getStackTrace()){
					sb.append("at ");
					sb.append(st.toString());
					sb.append("\n");
				}
				sb.append("'}");
				sb.append("'}");
				jsonString=sb.toString();
			}
			writer.println(jsonString);
		} catch (IOException ex1) {
			ex1.printStackTrace();
		} finally {
			writer.close();
		}
	}
	
	/*
	* sub class do with business in this method.
	*/
	protected abstract String createJson(HttpServletRequest request);
}



这里是用法:
	@RequestMapping("/json.htm") 	
	public void jsonMethod(HttpServletRequest request,HttpServletResponse response) {
		new JsonCallback() {
			protected String createJson(HttpServletRequest request) {
				return "{'name':'hello json'}";
			}
		}.doInController(request, response);
	}


分享到:
评论

相关推荐

    INSIST耳机驱动程序

    影极INSIST系列耳机驱动程序是专为该品牌耳机设计的一款重要软件组件,它在音频体验中扮演着不可或缺的角色。驱动程序本质上是操作系统与硬件设备之间的桥梁,使得计算机能够识别并有效利用硬件设备的功能。在本例中...

    Web服务器Mongrel2.zip

    Mongrel2 is also designed to be incredibly easy to automatically manage it as part of your infrastructure.Other web servers do some of these things, but they either do them in a bastardized way or ...

    INSIST_01 java

    package victory; public class INSIST_01 { public static void main (String [] args){ System.out.print("Insist_victory"); } }

    有关insist的用法整理.doc

    在使用 insist 时要注意它与相似表达 `stick to (doing) sth.` 和 `persist in (doing) sth.` 的区别: - `insist on (doing) sth.` 坚持某种做法、意见或主张。 - `stick to (doing) sth.` 坚持原则、计划、诺言等...

    spring data jpa 的Specifications动态查询

    Spring Data JPA是Spring框架的一个模块,它简化了Java应用程序对数据库的操作,通过提供自动的repository接口实现。在处理复杂的查询需求时,Spring Data JPA的`Specifications`接口提供了动态构建查询的能力,允许...

    spring date jpa

    Spring Data JPA是Spring框架的一个模块,用于简化Java Persistence API(JPA)的使用,它提供了与数据库交互的高级抽象。在Spring应用中整合Spring Data JPA,可以极大地提高开发效率,减少大量的DAO层代码。下面...

    spring-boot-06-data-jpa.zip

    《Spring Boot整合Spring Data JPA深度解析》 在现代Java开发中,Spring Boot以其便捷的配置、快速的应用启动以及强大的生态系统赢得了广泛的认可。而在数据访问层,Spring Data JPA则是Spring框架提供的一种高效且...

    Universal-USB-Installer

    interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a ...

    WizFlow网页编辑

    In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library ...

    hibernate-shards.jar

    In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library ...

    大学生英语竞赛(NECCS)A类研究生英语初赛真题2008年(无听力)-副本

    32. His father came home to dinner every night, and ______ in his life Jack felt that they were a real family. A. for the first time B. firstly C. at first time D. at first 33. After the robbery, the ...

    经典6级考试作文模板,不看不要后悔有

    4)What's more, living in school can save them a great deal of time on the way between home and school everyday, so they would be able to concentrate more time and energy on their academic work....

    insist用法和练习PPT学习教案.pptx

    insist用法和练习PPT学习教案.pptx

    自学考试英语试题及答案历年真题

    10. **介词短语辨析**:"in a way"意为"在某种程度上",适合表达"I must admit that it is a good thing in a way I did not get that post I applied for, though I was disappointed at the time.",即在某种程度...

    Site Reliability Engineering

    The overwhelming majority of a software system’s lifespan is spent in use, not in design or implementation. So, why does conventional wisdom insist that software engineers focus primarily on the ...

    BIND-LINUX64位包

    a way to specify the local address for use when sending update packets. [RT #34811] "named-checkconf -z" now checks zones of type hint as well as master. [RT #35046] Update config.guess and ...

    英语A级资料

    26. **adhere to** ( = abide by, conform to, comply with, cling to, insist on, persist in, observe, opinion, belief):坚持,遵守,表示对原则、信念或规则的忠诚。 27. **adjacent** ( = next to, close to)...

    spring-boot-redis.zip

    在IT行业中,Spring Boot是一个非常流行的微服务框架,它简化了Spring应用的初始搭建以及开发过程。而Redis则是一款高性能的键值数据存储系统,常用于缓存、消息队列等场景。本教程将深入探讨如何在Spring Boot项目...

    zcxiaobao#everyday-insist#css清除浮动的几种方法(2020.01.08)1

    分析:清除浮动大致有两种方案:clear、触发 BFC额外标签法: 在浮动元素平级的最后面添加额外元素,例如:<div class="clearfix"></d

Global site tag (gtag.js) - Google Analytics