Skip to content

Commit 85ad446

Browse files
nguyennamthaipivovarit
authored andcommitted
First commit of protocol buffers. (eugenp#452)
1 parent d9223b2 commit 85ad446

7 files changed

Lines changed: 2972 additions & 0 deletions

File tree

‎spring-protobuf/pom.xml‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-protobuf</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
<name>spring-protobuf</name>
8+
9+
<parent>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter-parent</artifactId>
12+
<version>1.2.4.RELEASE</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.google.protobuf</groupId>
18+
<artifactId>protobuf-java</artifactId>
19+
<version>3.0.0-beta-3</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.googlecode.protobuf-java-format</groupId>
23+
<artifactId>protobuf-java-format</artifactId>
24+
<version>1.4</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-web</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.apache.httpcomponents</groupId>
39+
<artifactId>httpclient</artifactId>
40+
<version>4.5.2</version>
41+
</dependency>
42+
</dependencies>
43+
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.protobuf;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
import com.baeldung.protobuf.BaeldungTraining.Course;
15+
import com.baeldung.protobuf.BaeldungTraining.Student;
16+
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber;
17+
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneType;
18+
19+
@SpringBootApplication
20+
public class Application {
21+
@Bean
22+
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
23+
return new RestTemplate(Arrays.asList(hmc));
24+
}
25+
26+
@Bean
27+
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
28+
return new ProtobufHttpMessageConverter();
29+
}
30+
31+
@Bean
32+
public CourseRepository createDummyCourses() {
33+
Map<Integer, Course> dummy = new HashMap<>();
34+
35+
Course course1 = Course.newBuilder().setId(1).setCourseName("REST with Spring").addAllStudent(createDummyStudents()).build();
36+
Course course2 = Course.newBuilder().setId(2).setCourseName("Learn Spring Security").addAllStudent(new ArrayList<Student>()).build();
37+
38+
dummy.put(course1.getId(), course1);
39+
dummy.put(course2.getId(), course2);
40+
41+
return new CourseRepository(dummy);
42+
}
43+
44+
private List<Student> createDummyStudents() {
45+
List<Student> studentList = new ArrayList<>();
46+
47+
PhoneNumber phone1 = createPhone("123456", PhoneType.MOBILE);
48+
Student student1 = createStudent(1, "John", "Doe", "john.doe@baeldung.com", Arrays.asList(phone1));
49+
50+
PhoneNumber phone2 = createPhone("234567", PhoneType.LANDLINE);
51+
Student student2 = createStudent(2, "Richard", "Roe", "richard.roe@baeldung.com", Arrays.asList(phone2));
52+
53+
PhoneNumber phone3_1 = createPhone("345678", PhoneType.MOBILE);
54+
PhoneNumber phone3_2 = createPhone("456789", PhoneType.LANDLINE);
55+
Student student3 = createStudent(3, "Jane", "Doe", "jane.doe@baeldung.com", Arrays.asList(phone3_1, phone3_2));
56+
57+
studentList.addAll(Arrays.asList(student1, student2, student3));
58+
return studentList;
59+
}
60+
61+
private Student createStudent(int id, String firstName, String lastName, String email, List<PhoneNumber> phones) {
62+
return Student.newBuilder().setId(id).setFirstName(firstName).setLastName(lastName).setEmail(email).addAllPhone(phones).build();
63+
}
64+
65+
private PhoneNumber createPhone(String number, PhoneType type) {
66+
return PhoneNumber.newBuilder().setNumber(number).setType(type).build();
67+
}
68+
}

0 commit comments

Comments
 (0)