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