Skip to content

Commit 9398a29

Browse files
committed
Merge branch 'master' of https://github.com/bdragan/tutorials into bdragan-master
2 parents 2b2022c + 57411aa commit 9398a29

6 files changed

Lines changed: 732 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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.baeldung.config.converter;
2+
3+
import java.io.IOException;
4+
5+
import org.baeldung.web.dto.Foo;
6+
import org.springframework.http.HttpInputMessage;
7+
import org.springframework.http.HttpOutputMessage;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.http.converter.AbstractHttpMessageConverter;
10+
11+
import com.esotericsoftware.kryo.Kryo;
12+
import com.esotericsoftware.kryo.io.Input;
13+
import com.esotericsoftware.kryo.io.Output;
14+
15+
/**
16+
* An {@code HttpMessageConverter} that can read and write Kryo messages.
17+
*/
18+
public class KryoHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
19+
20+
public static final MediaType KRYO = new MediaType("application", "x-kryo");
21+
22+
private static final ThreadLocal<Kryo> kryoThreadLocal = new ThreadLocal<Kryo>() {
23+
@Override
24+
protected Kryo initialValue() {
25+
final Kryo kryo = new Kryo();
26+
kryo.register(Foo.class, 1);
27+
return kryo;
28+
}
29+
};
30+
31+
public KryoHttpMessageConverter() {
32+
super(KRYO);
33+
}
34+
35+
@Override
36+
protected boolean supports(final Class<?> clazz) {
37+
return Object.class.isAssignableFrom(clazz);
38+
}
39+
40+
@Override
41+
protected Object readInternal(final Class<? extends Object> clazz, final HttpInputMessage inputMessage) throws IOException {
42+
final Input input = new Input(inputMessage.getBody());
43+
return kryoThreadLocal.get().readClassAndObject(input);
44+
}
45+
46+
@Override
47+
protected void writeInternal(final Object object, final HttpOutputMessage outputMessage) throws IOException {
48+
final Output output = new Output(outputMessage.getBody());
49+
kryoThreadLocal.get().writeClassAndObject(output, object);
50+
output.flush();
51+
}
52+
53+
@Override
54+
protected MediaType getDefaultContentType(final Object object) {
55+
return KRYO;
56+
}
57+
}

‎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)