sprintmvc+protostuff
来源:互联网 发布:rsa加密算法 c语言 编辑:程序博客网 时间:2024/06/02 06:14
server:
pom 增加protostuff
<dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-collectionschema</artifactId> <version>1.3.8</version></dependency><dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-runtime</artifactId> <version>1.3.8</version></dependency><dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-api</artifactId> <version>1.3.8</version></dependency><dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-core</artifactId> <version>1.3.8</version></dependency><dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-json</artifactId> <version>1.3.8</version></dependency><dependency> <groupId>net.jpountz.lz4</groupId> <artifactId>lz4</artifactId> <version>1.3.0</version></dependency><dependency> <groupId>org.xerial.snappy</groupId> <artifactId>snappy-java</artifactId> <version>1.1.2.1</version> <type>jar</type> <scope>compile</scope></dependency>
message convert:
@Beanpublic RequestMappingHandlerAdapter requestMappingHandlerAdapter() { RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter(); adapter.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); adapter.getMessageConverters().add(new ProtostuffHttpMessageConverter()); return adapter;}
package spring;import com.google.common.base.Stopwatch;import io.protostuff.LinkedBuffer;import io.protostuff.ProtobufIOUtil;import io.protostuff.Schema;import io.protostuff.runtime.RuntimeSchema;import net.jpountz.lz4.LZ4BlockOutputStream;import org.apache.commons.io.IOUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;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 org.xerial.snappy.SnappyOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.nio.charset.Charset;import java.util.zip.GZIPOutputStream;/** * <p> * An {@code HttpMessageConverter} that can read and write * <a href="https://developers.google.com/protocol-buffers/">Google Protocol Buffer (Protobuf)</a> messages using the * open-source <a href="http://http://www.protostuff.io">Protostuff library</a>. Its advantage over native Protobuf * serialization and deserialization is that it can work with normal {@code POJO}s, as compared to the native * implementation that requires the objects to implement the {@code Message} interface from the Protobuf Java library. * This allows applications to use Protobuf with existing classes instead of having to re-generate them using the * Protobuf compiler. * </p> * <p> * Supports the {@code application/x-protobuf} media type. Regular Spring MVC application clients can use this as the * media type for the {@code Accept} and {@code Content-Type} HTTP headers for exchanging information as Protobuf * messages. * </p> */public class ProtostuffHttpMessageConverter extends AbstractHttpMessageConverter<Object> { private Logger logger = LoggerFactory.getLogger(getClass()); public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); public static final MediaType MEDIA_TYPE = new MediaType("application", "x-protobuf", DEFAULT_CHARSET); public static final MediaType MEDIA_TYPE_LZ4 = new MediaType("application", "x-protobuf-lz4", DEFAULT_CHARSET); public static final MediaType MEDIA_TYPE_GZIP = new MediaType("application", "x-protobuf-gzip", DEFAULT_CHARSET); public static final MediaType MEDIA_TYPE_SNAPPY = new MediaType("application", "x-protobuf-snappy", DEFAULT_CHARSET); /** * Construct a new instance. */ public ProtostuffHttpMessageConverter() { super(MEDIA_TYPE, MEDIA_TYPE_LZ4, MEDIA_TYPE_GZIP, MEDIA_TYPE_SNAPPY); } /** * {@inheritDoc} */ @Override public boolean canRead(final Class<?> clazz, final MediaType mediaType) { return canRead(mediaType); } /** * {@inheritDoc} */ @Override public boolean canWrite(final Class<?> clazz, final MediaType mediaType) { return canWrite(mediaType); } /** * {@inheritDoc} */ @Override protected Object readInternal(final Class<?> clazz, final HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { if (MEDIA_TYPE.isCompatibleWith(inputMessage.getHeaders().getContentType())) { final Schema<?> schema = RuntimeSchema.getSchema(clazz); final Object value = schema.newMessage(); try (final InputStream stream = inputMessage.getBody()) { ProtobufIOUtil.mergeFrom(stream, value, (Schema<Object>) schema); return value; } } throw new HttpMessageNotReadableException( "Unrecognized HTTP media type " + inputMessage.getHeaders().getContentType().getType() + "."); } /** * {@inheritDoc} */ @Override protected boolean supports(final Class<?> clazz) { // Should not be called, since we override canRead/canWrite. throw new UnsupportedOperationException(); } /** * {@inheritDoc} */ @Override protected void writeInternal(final Object o, final HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { logger.info("Current type: {}", outputMessage.getHeaders().getContentType()); Stopwatch stopwatch = Stopwatch.createStarted(); OutputStream stream = null; try { stream = outputMessage.getBody(); if (MEDIA_TYPE.isCompatibleWith(outputMessage.getHeaders().getContentType())) { } else if (MEDIA_TYPE_GZIP.isCompatibleWith(outputMessage.getHeaders().getContentType())) { stream = new GZIPOutputStream(stream); } else if (MEDIA_TYPE_LZ4.isCompatibleWith(outputMessage.getHeaders().getContentType())) { stream = new LZ4BlockOutputStream(stream); } else if (MEDIA_TYPE_SNAPPY.isCompatibleWith(outputMessage.getHeaders().getContentType())) { stream = new SnappyOutputStream(stream); } else { throw new HttpMessageNotWritableException( "Unrecognized HTTP media type " + outputMessage.getHeaders().getContentType().getType() + "."); } ProtobufIOUtil.writeTo(stream, o, RuntimeSchema.getSchema((Class<Object>) o.getClass()), LinkedBuffer.allocate()); stream.flush(); } finally { IOUtils.closeQuietly(stream); } logger.info("Output spend {}", stopwatch.toString()); }}
controller:
还是按照原有controller写
@Controller()@RequestMapping("/protostuff")public class ProtostufTest { @ResponseBody @RequestMapping("/stub") public StubResponse testStub(@RequestBody StubRequest request) { StubResponse stubResponse = new StubResponse(); stubResponse.setA(request.getA()); stubResponse.setB(request.getB()); stubResponse.setC(request.getC()); return stubResponse; } @ResponseBody @RequestMapping("/check") public String check() { return "OK"; }}
client 端:
@Test@Ignorepublic void test() throws IOException { StubRequest stubRequest = new StubRequest(); stubRequest.setA(1); stubRequest.setB(RandomStringUtils.randomAlphabetic(100000)); stubRequest.setC(BigDecimal.ONE); // json test HttpRequest httpRequest2 = new HttpRequest("http://localhost:8198/protostuff/check", HttpRequest.METHOD_POST); String resultStr = httpRequest2.connectTimeout(100000).readTimeout(10000) .contentType("application/json").send(JSON.toJSONString (stubRequest)).body(); assertEquals("OK", resultStr); // protostuff lz-4 test HttpRequest httpRequest = new HttpRequest("http://localhost:8198/protostuff/stub", HttpRequest.METHOD_POST); byte[] result = httpRequest.connectTimeout(100000) .readTimeout(10000) .accept("application/x-protobuf-lz4") .contentType("application/json") .send(JSON.toJSONString(stubRequest)) .bytes(); LZ4BlockInputStream inputStream = new LZ4BlockInputStream(new ByteArrayInputStream(result)); byte[] buffer = IOUtils.toByteArray(inputStream); Schema<StubResponse> schema = RuntimeSchema.getSchema(StubResponse.class); StubResponse value = schema.newMessage(); ProtobufIOUtil.mergeFrom(buffer, value, schema); assertEquals(stubRequest.getB(), value.getB()); //protostuff HttpRequest httpRequest3 = new HttpRequest("http://localhost:8198/protostuff/stub", HttpRequest.METHOD_POST); byte[] result3 = httpRequest3.connectTimeout(100000) .readTimeout(10000) .accept("application/x-protobuf") .contentType("application/json") .send(JSON.toJSONString(stubRequest)) .bytes(); schema = RuntimeSchema.getSchema(StubResponse.class); value = schema.newMessage(); ProtobufIOUtil.mergeFrom(result3, value, schema); assertEquals(stubRequest.getB(), value.getB());}
github:https://github.com/fatshaw/protostuffdemo
0 0
- sprintmvc+protostuff
- google protostuff
- Protostuff详解
- Protostuff开发
- Protostuff详解
- Protostuff简介
- protostuff使用
- Protostuff序列化
- ProtoStuff序列化工具
- Protostuff序列化
- protostuff报错
- Protostuff序列化
- protostuff工具的使用
- Protostuff序列化
- 通过protostuff实现动态proto
- Protostuff序列化工具类
- protostuff序列化工具类
- String之Protostuff序列化踩坑
- “匠人精神”6大内涵:德国登峰造极制造业的成功秘诀
- callback(回调函数)
- 打开excel自动自动打开personal.xlsb表格文件
- hdu 2035 人见人爱A^B
- 登录帐号管理含义及运用
- sprintmvc+protostuff
- ubuntu 固定ip
- 不能移动的石子合并
- hbase filter shell用法
- 在cygwin上面安装gcc
- Java 9终于要包含Jigsaw项目了
- 架构之路--性能探究
- git教程
- Android产生圆角图片Bitmap