Java tutorial
/* * Copyright 1999-2004 Alibaba.com All right reserved. This software is the confidential and proprietary information of * Alibaba.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only * in accordance with the terms of the license agreement you entered into with Alibaba.com. */ package com.alibaba.webmvc.extension; import com.alibaba.fastjson.JSON; 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.GenericHttpMessageConverter; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Type; import java.nio.charset.Charset; /** * Created with IntelliJ IDEA. * * @author zizhi.zhzzh * Date: 7/24/14 * Time: 11:39 PM */ public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> implements GenericHttpMessageConverter<Object> { public static final Charset DEFAULT_CHARSET = JSONs.DEFAULT_CHARSET; private boolean prettyPrint; /** * Construct a new {@code MappingJacksonHttpMessageConverter}. */ public FastJsonHttpMessageConverter() { super(new MediaType("application", "json", DEFAULT_CHARSET), new MediaType("application", "*+json", DEFAULT_CHARSET)); } @Override public boolean canRead(Class<?> clazz, MediaType mediaType) { return canRead(clazz, null, mediaType); } public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) { return canRead(mediaType); } @Override public boolean canWrite(Class<?> clazz, MediaType mediaType) { return canWrite(mediaType); } @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 { StringBuilder sb = new StringBuilder(); InputStream in = inputMessage.getBody(); byte[] buf = new byte[8000]; int len; while ((len = in.read(buf)) >= 0) { sb.append(new String(buf, 0, len, DEFAULT_CHARSET)); } return JSON.parseObject(sb.toString(), clazz); } public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { return readInternal(contextClass, inputMessage); } @Override protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { MediaType mediaType = outputMessage.getHeaders().getContentType(); Charset charset = mediaType.getCharSet(); if (charset == null) { charset = DEFAULT_CHARSET; } JSONs.writeTo(outputMessage.getBody(), object, charset, prettyPrint); } public void setPrettyPrint(boolean prettyPrint) { this.prettyPrint = prettyPrint; } }