日期:2014-05-16  浏览次数:20416 次

SpringMVC杂记(八) 使用阿里巴巴的fastjson
1) 国产开源软件要支持的
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.1.22</version>
</dependency>


2) spring没有提供相应的HttpMessageConverter可以自己写一个。
package com.alibaba.fastjson.spring.support;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class MappingFastJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> {

	public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
	
	// fastjson特性参数
	private SerializerFeature[] serializerFeature;

	public SerializerFeature[] getSerializerFeature() {
		return serializerFeature;
	}

	public void setSerializerFeature(SerializerFeature[] serializerFeature) {
		this.serializerFeature = serializerFeature;
	}

	public MappingFastJsonHttpMessageConverter() {
		super(new MediaType("application", "json", DEFAULT_CHARSET));
	}

	@Override
	public boolean canRead(Class<?> clazz, MediaType mediaType) {
		// JavaType javaType = getJavaType(clazz);
		// return this.objectMapper.canDeserialize(javaType) &&
		// canRead(mediaType);
		return true;
	}

	@Override
	public boolean canWrite(Class<?> clazz, MediaType mediaType) {
		// return this.objectMapper.canSerialize(clazz) && canWrite(mediaType);
		return true;
	}

	@Override
	protected boolean supports(Class<?> clazz) {
		// should not be called, since we override canRead/Write instead
		throw new UnsupportedOperationException();
	}

	@Override
	protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
			throws IOException, HttpMessageNotReadableException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int i;
		while ((i = inputMessage.getBody().read()) != -1) {
			baos.write(i);
		}
		return JSON.parseArray(baos.toString(), clazz);
	}

	@Override
	protected void writeInternal(Object o, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException {
		String jsonString = JSON.toJSONString(o, serializerFeature);
		OutputStream out = outputMessage.getBody();
		out.write(jsonString.getBytes(DEFAULT_CHARSET));
		out.flush();
	}

}


3) 配置
<bean class="com.alibaba.fastjson.spring.support.MappingFastJsonHttpMessageConverter">
	<property name="supportedMediaTypes" value="application/json" />
	<property name="serializerFeature">
		<array>
			<value>WriteMapNullValue</value>
			<value>QuoteFieldNames</value>
		</array>
	</property>
</bean>
1 楼 Jstar 2012-08-15  
配置好这个后在controller里面还需要加什么其他注解了么,如果有些方法是返回json的,有些又不想返回json的该怎么做。
2 楼 yingzhor 2012-08-15  
@ResponseBody 标注加到Controller 方法上返回的即为json

不加,那就和普通的Controller 没有什么不同。

如:
返回String的是逻辑视图名。
ModelAndView 模型和逻辑视图一起返回。
3 楼 Jstar 2012-08-16  
THANKS~~~~~~

另外我想问下,返回的JSON在前端页面是怎样绑定, 用datalink? 有没有例子。