PageRenderTime 5316ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/tts/component/converter/JsonHttpMessageConverter.java

https://gitlab.com/Fish-Potato/TTStreet-core
Java | 83 lines | 64 code | 14 blank | 5 comment | 3 complexity | 6ebd448879169c3c97a7168ddf82f03c MD5 | raw file
  1. package com.tts.component.converter;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.serializer.SerializerFeature;
  4. import com.tts.util.JsonUtil;
  5. import org.springframework.http.HttpInputMessage;
  6. import org.springframework.http.HttpOutputMessage;
  7. import org.springframework.http.MediaType;
  8. import org.springframework.http.converter.AbstractHttpMessageConverter;
  9. import org.springframework.http.converter.HttpMessageNotReadableException;
  10. import org.springframework.http.converter.HttpMessageNotWritableException;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.util.Base64Utils;
  13. import java.io.ByteArrayOutputStream;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.OutputStream;
  17. import java.nio.charset.Charset;
  18. /**
  19. * Created by tts on 2016/5/4.
  20. */
  21. @Service
  22. public class JsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
  23. public static final Charset UTF8 = Charset.forName("UTF-8");
  24. private Charset charset;
  25. private SerializerFeature[] features;
  26. public JsonHttpMessageConverter() {
  27. super(new MediaType[]{new MediaType("application", "json", UTF8), new MediaType("application", "*+json", UTF8)});
  28. this.charset = UTF8;
  29. this.features = new SerializerFeature[0];
  30. }
  31. protected boolean supports(Class<?> clazz) {
  32. return true;
  33. }
  34. public Charset getCharset() {
  35. return this.charset;
  36. }
  37. public void setCharset(Charset charset) {
  38. this.charset = charset;
  39. }
  40. public SerializerFeature[] getFeatures() {
  41. return this.features;
  42. }
  43. public void setFeatures(SerializerFeature... features) {
  44. this.features = features;
  45. }
  46. public Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
  47. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  48. InputStream in = inputMessage.getBody();
  49. byte[] buf = new byte[1024];
  50. while(true) {
  51. int bytes = in.read(buf);
  52. if(bytes == -1) {
  53. // base64解码
  54. byte[] bytes1 = Base64Utils.decode(baos.toByteArray());
  55. return JsonUtil.toObject(new String(bytes1,0,bytes1.length),clazz);
  56. }
  57. if(bytes > 0) {
  58. baos.write(buf, 0, bytes);
  59. }
  60. }
  61. }
  62. protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
  63. OutputStream out = outputMessage.getBody();
  64. String text = JSON.toJSONString(obj, this.features);
  65. byte[] bytes = text.getBytes(this.charset);
  66. // base64编码
  67. out.write(Base64Utils.encode(bytes));
  68. }
  69. }