Skip to content

Commit 27d58fc

Browse files
committed
Code examples for the 'Binary Data Formats in a Spring REST API'
article.
1 parent 70322e1 commit 27d58fc

6 files changed

Lines changed: 748 additions & 2 deletions

File tree

‎spring-rest/pom.xml‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,16 @@
155155
<artifactId>spring-test</artifactId>
156156
<version>${spring.version}</version>
157157
</dependency>
158-
158+
<dependency>
159+
<groupId>com.google.protobuf</groupId>
160+
<artifactId>protobuf-java</artifactId>
161+
<version>2.6.1</version>
162+
</dependency>
163+
<dependency>
164+
<groupId>com.esotericsoftware.kryo</groupId>
165+
<artifactId>kryo</artifactId>
166+
<version>2.24.0</version>
167+
</dependency>
159168
</dependencies>
160169

161170
<build>

‎spring-rest/src/main/java/org/baeldung/config/WebConfig.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import java.text.SimpleDateFormat;
44
import java.util.List;
55

6+
import org.baeldung.config.converter.KryoHttpMessageConverter;
67
import org.springframework.context.annotation.ComponentScan;
78
import org.springframework.context.annotation.Configuration;
89
import org.springframework.http.converter.HttpMessageConverter;
910
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
1011
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
12+
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
1113
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
1214
import org.springframework.oxm.xstream.XStreamMarshaller;
1315
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@@ -33,7 +35,8 @@ public void configureMessageConverters(final List<HttpMessageConverter<?>> messa
3335
builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy hh:mm"));
3436
messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
3537
// messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));
36-
38+
messageConverters.add(new ProtobufHttpMessageConverter());
39+
messageConverters.add(new KryoHttpMessageConverter());
3740
super.configureMessageConverters(messageConverters);
3841
}
3942

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2002-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.baeldung.config.converter;
18+
19+
import java.io.IOException;
20+
21+
import org.baeldung.web.dto.Foo;
22+
import org.springframework.http.HttpInputMessage;
23+
import org.springframework.http.HttpOutputMessage;
24+
import org.springframework.http.MediaType;
25+
import org.springframework.http.converter.AbstractHttpMessageConverter;
26+
27+
import com.esotericsoftware.kryo.Kryo;
28+
import com.esotericsoftware.kryo.io.Input;
29+
import com.esotericsoftware.kryo.io.Output;
30+
31+
/**
32+
* An {@code HttpMessageConverter} that can read and write Kryo messages.
33+
*/
34+
public class KryoHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
35+
36+
public static final MediaType KRYO = new MediaType("application", "x-kryo");
37+
38+
private static final ThreadLocal<Kryo> kryoThreadLocal = new ThreadLocal<Kryo>() {
39+
@Override
40+
protected Kryo initialValue() {
41+
final Kryo kryo = new Kryo();
42+
kryo.register(Foo.class, 1);
43+
return kryo;
44+
}
45+
};
46+
47+
public KryoHttpMessageConverter() {
48+
super(KRYO);
49+
}
50+
51+
@Override
52+
protected boolean supports(final Class<?> clazz) {
53+
return Object.class.isAssignableFrom(clazz);
54+
}
55+
56+
@Override
57+
protected Object readInternal(final Class<? extends Object> clazz, final HttpInputMessage inputMessage) throws IOException {
58+
final Input input = new Input(inputMessage.getBody());
59+
return kryoThreadLocal.get().readClassAndObject(input);
60+
}
61+
62+
@Override
63+
protected void writeInternal(final Object object, final HttpOutputMessage outputMessage) throws IOException {
64+
final Output output = new Output(outputMessage.getBody());
65+
kryoThreadLocal.get().writeClassAndObject(output, object);
66+
output.flush();
67+
}
68+
69+
@Override
70+
protected MediaType getDefaultContentType(final Object object) {
71+
return KRYO;
72+
}
73+
}

‎spring-rest/src/main/java/org/baeldung/web/controller/FooController.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
55

66
import org.baeldung.web.dto.Foo;
7+
import org.baeldung.web.dto.FooProtos;
78
import org.springframework.http.HttpStatus;
89
import org.springframework.stereotype.Controller;
910
import org.springframework.web.bind.annotation.PathVariable;
@@ -38,4 +39,9 @@ public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo
3839
return foo;
3940
}
4041

42+
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" })
43+
@ResponseBody
44+
public FooProtos.Foo findProtoById(@PathVariable final long id) {
45+
return FooProtos.Foo.newBuilder().setId(1).setName("Foo Name").build();
46+
}
4147
}

0 commit comments

Comments
 (0)