package net.joystart.common.controller; import java.io.IOException; import java.io.PrintWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import javax.servlet.http.HttpServletResponse; import net.joystart.common.util.ConstantUtil; import net.joystart.common.util.MessageResult; import net.joystart.common.util.date.DateSerializer; import net.joystart.common.util.date.DateToStringSerializer; import net.joystart.common.util.date.DateToStringYYYYMMDDSerializer; import net.joystart.common.util.gsonAdapter.NullStringToEmptyAdapterFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonSerializer; import com.google.gson.TypeAdapter; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonWriter; /** * * @author yang yanqing * @date 2016年7月7日 * BaseController 基础控制器 */ @Controller public class BaseController { protected Logger logger = LoggerFactory.getLogger(this.getClass()); @Deprecated protected int pageSize = 20; @Deprecated protected int pageNumber = 1; public String toJson(Object obj){ GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(java.util.Date.class, new DateToStringYYYYMMDDSerializer()); Gson gson = gb.create(); return gson.toJson(obj); } public void out(HttpServletResponse response,Object result){ try { //浏览器用utf8来解析返回的数据 response.setHeader("Content-type", "text/html;charset=UTF-8"); //servlet用UTF-8转码,而不是用默认的ISO8859 response.setCharacterEncoding(ConstantUtil.UTF_CODE); PrintWriter writer = response.getWriter(); GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(java.util.Date.class, new DateSerializer()).setDateFormat(DateFormat.LONG); Gson gson = gb.create(); writer.print(gson.toJson(result) ); } catch (IOException e) { e.printStackTrace(); } finally{ } } public void outEd(HttpServletResponse response,Object result){ try { //浏览器用utf8来解析返回的数据 response.setHeader("Content-type", "application/json;charset=UTF-8"); response.setContentType("application/json;charset=UTF-8"); //servlet用UTF-8转码,而不是用默认的ISO8859 response.setCharacterEncoding(ConstantUtil.UTF_CODE); PrintWriter writer = response.getWriter(); GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(java.util.Date.class, new DateSerializer()).setDateFormat(DateFormat.LONG); Gson gson = gb.create(); System.out.println(gson.toJson(result)); writer.print(gson.toJson(result)); } catch (IOException e) { e.printStackTrace(); } finally{ } } public void out(HttpServletResponse response,Map messageMap){ try { //浏览器用utf8来解析返回的数据 response.setHeader("Content-type", "text/html;charset=UTF-8"); //servlet用UTF-8转码,而不是用默认的ISO8859 response.setCharacterEncoding(ConstantUtil.UTF_CODE); PrintWriter writer = response.getWriter(); GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(java.util.Date.class, new DateSerializer()).setDateFormat(DateFormat.LONG); Gson gson = gb.create(); writer.print(gson.toJson(messageMap) ); } catch (IOException e) { e.printStackTrace(); }finally{ } } public void webOut(HttpServletResponse response,Map messageMap){ try { //浏览器用utf8来解析返回的数据 response.setHeader("Content-type", "text/html;charset=UTF-8"); //servlet用UTF-8转码,而不是用默认的ISO8859 response.setCharacterEncoding(ConstantUtil.UTF_CODE); PrintWriter writer = response.getWriter(); GsonBuilder gb = new GsonBuilder(); //gb.registerTypeAdapter(java.util.Date.class, new DateSerializer()).setDateFormat(DateFormat.LONG); gb.registerTypeAdapter(java.util.Date.class, new DateToStringSerializer()); Gson gson = gb.create(); writer.print(gson.toJson(messageMap) ); } catch (IOException e) { e.printStackTrace(); }finally{ } } public void webOut(HttpServletResponse response,Object result){ try { //浏览器用utf8来解析返回的数据 response.setHeader("Content-type", "text/html;charset=UTF-8"); //servlet用UTF-8转码,而不是用默认的ISO8859 response.setCharacterEncoding(ConstantUtil.UTF_CODE); PrintWriter writer = response.getWriter(); GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(java.util.Date.class, new DateToStringSerializer()); Gson gson = gb.create(); writer.print(gson.toJson(result) ); } catch (IOException e) { e.printStackTrace(); }finally{ } } /** * 对象属性值为null时(只支持String,Integer类型),json中显示该属性,并将该属性值显示为"" * @param response * @param result */ @SuppressWarnings({ "unchecked", "rawtypes" }) public void outWithNull(HttpServletResponse response,MessageResult result){ try { //浏览器用utf8来解析返回的数据 response.setHeader("Content-type", "text/html;charset=UTF-8"); //servlet用UTF-8转码,而不是用默认的ISO8859 response.setCharacterEncoding("UTF-8"); JsonWriter out = new JsonWriter(response.getWriter()); GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).registerTypeAdapter(java.util.Date.class, new DateSerializer()).setDateFormat(DateFormat.LONG); Gson gson = gb.create(); // Gson g = new GsonBuilder().serializeNulls().create(); TypeAdapter< MessageResult > adapter = gson.getAdapter(TypeToken.get(MessageResult.class)); ((TypeAdapter) adapter).write(out, result); } catch (IOException e) { e.printStackTrace(); }finally{ } } @InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageNumber() { return pageNumber; } public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; } }