First commit of protocol buffers. (#452)
This commit is contained in:
parent
d9223b24fb
commit
85ad446ccf
|
@ -0,0 +1,43 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-protobuf</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-protobuf</name>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.2.4.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.protobuf</groupId>
|
||||
<artifactId>protobuf-java</artifactId>
|
||||
<version>3.0.0-beta-3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.protobuf-java-format</groupId>
|
||||
<artifactId>protobuf-java-format</artifactId>
|
||||
<version>1.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.protobuf;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.baeldung.protobuf.BaeldungTraining.Course;
|
||||
import com.baeldung.protobuf.BaeldungTraining.Student;
|
||||
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber;
|
||||
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneType;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
@Bean
|
||||
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
|
||||
return new RestTemplate(Arrays.asList(hmc));
|
||||
}
|
||||
|
||||
@Bean
|
||||
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
|
||||
return new ProtobufHttpMessageConverter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CourseRepository createDummyCourses() {
|
||||
Map<Integer, Course> dummy = new HashMap<>();
|
||||
|
||||
Course course1 = Course.newBuilder().setId(1).setCourseName("REST with Spring").addAllStudent(createDummyStudents()).build();
|
||||
Course course2 = Course.newBuilder().setId(2).setCourseName("Learn Spring Security").addAllStudent(new ArrayList<Student>()).build();
|
||||
|
||||
dummy.put(course1.getId(), course1);
|
||||
dummy.put(course2.getId(), course2);
|
||||
|
||||
return new CourseRepository(dummy);
|
||||
}
|
||||
|
||||
private List<Student> createDummyStudents() {
|
||||
List<Student> studentList = new ArrayList<>();
|
||||
|
||||
PhoneNumber phone1 = createPhone("123456", PhoneType.MOBILE);
|
||||
Student student1 = createStudent(1, "John", "Doe", "john.doe@baeldung.com", Arrays.asList(phone1));
|
||||
|
||||
PhoneNumber phone2 = createPhone("234567", PhoneType.LANDLINE);
|
||||
Student student2 = createStudent(2, "Richard", "Roe", "richard.roe@baeldung.com", Arrays.asList(phone2));
|
||||
|
||||
PhoneNumber phone3_1 = createPhone("345678", PhoneType.MOBILE);
|
||||
PhoneNumber phone3_2 = createPhone("456789", PhoneType.LANDLINE);
|
||||
Student student3 = createStudent(3, "Jane", "Doe", "jane.doe@baeldung.com", Arrays.asList(phone3_1, phone3_2));
|
||||
|
||||
studentList.addAll(Arrays.asList(student1, student2, student3));
|
||||
return studentList;
|
||||
}
|
||||
|
||||
private Student createStudent(int id, String firstName, String lastName, String email, List<PhoneNumber> phones) {
|
||||
return Student.newBuilder().setId(id).setFirstName(firstName).setLastName(lastName).setEmail(email).addAllPhone(phones).build();
|
||||
}
|
||||
|
||||
private PhoneNumber createPhone(String number, PhoneType type) {
|
||||
return PhoneNumber.newBuilder().setNumber(number).setType(type).build();
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.protobuf;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.protobuf.BaeldungTraining.Course;
|
||||
|
||||
@RestController
|
||||
public class CourseController {
|
||||
@Autowired
|
||||
CourseRepository courseRepo;
|
||||
|
||||
@RequestMapping("/courses/{id}")
|
||||
Course customer(@PathVariable Integer id) {
|
||||
return courseRepo.getCourse(id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.protobuf;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.baeldung.protobuf.BaeldungTraining.Course;
|
||||
|
||||
public class CourseRepository {
|
||||
Map<Integer, Course> courses;
|
||||
|
||||
public CourseRepository (Map<Integer, Course> courses) {
|
||||
this.courses = courses;
|
||||
}
|
||||
|
||||
public Course getCourse(int id) {
|
||||
return courses.get(id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
syntax = "proto3";
|
||||
|
||||
package baeldung;
|
||||
|
||||
option java_package = "com.baeldung.protobuf";
|
||||
option java_outer_classname = "BaeldungTraining";
|
||||
|
||||
message Course {
|
||||
int32 id = 1;
|
||||
string course_name = 2;
|
||||
repeated Student student = 3;
|
||||
}
|
||||
|
||||
message Student {
|
||||
int32 id = 1;
|
||||
string first_name = 2;
|
||||
string last_name = 3;
|
||||
string email = 4;
|
||||
repeated PhoneNumber phone = 5;
|
||||
|
||||
message PhoneNumber {
|
||||
string number = 1;
|
||||
PhoneType type = 2;
|
||||
}
|
||||
|
||||
enum PhoneType {
|
||||
MOBILE = 0;
|
||||
LANDLINE = 1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package com.baeldung.protobuf;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.WebIntegrationTest;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.protobuf.BaeldungTraining.Course;
|
||||
import com.googlecode.protobuf.format.JsonFormat;
|
||||
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@WebIntegrationTest
|
||||
public class ApplicationTest {
|
||||
private static final String COURSE1_URL = "http://localhost:8080/courses/1";
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void whenUsingRestTemplate_thenSucceed() {
|
||||
ResponseEntity<Course> course = restTemplate.getForEntity(COURSE1_URL, Course.class);
|
||||
assertResponse(course.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingHttpClient_thenSucceed() throws IOException {
|
||||
InputStream responseStream = executeHttpRequest(COURSE1_URL);
|
||||
String jsonOutput = convertProtobufMessageStreamToJsonString(responseStream);
|
||||
assertResponse(jsonOutput);
|
||||
}
|
||||
|
||||
private InputStream executeHttpRequest(String url) throws IOException {
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
HttpGet request = new HttpGet(url);
|
||||
HttpResponse httpResponse = httpClient.execute(request);
|
||||
return httpResponse.getEntity().getContent();
|
||||
}
|
||||
|
||||
private String convertProtobufMessageStreamToJsonString(InputStream protobufStream) throws IOException {
|
||||
JsonFormat jsonFormat = new JsonFormat();
|
||||
Course course = Course.parseFrom(protobufStream);
|
||||
return jsonFormat.printToString(course);
|
||||
}
|
||||
|
||||
private void assertResponse(String response) {
|
||||
assertThat(response, containsString("id"));
|
||||
assertThat(response, containsString("course_name"));
|
||||
assertThat(response, containsString("REST with Spring"));
|
||||
assertThat(response, containsString("student"));
|
||||
assertThat(response, containsString("first_name"));
|
||||
assertThat(response, containsString("last_name"));
|
||||
assertThat(response, containsString("email"));
|
||||
assertThat(response, containsString("john.doe@baeldung.com"));
|
||||
assertThat(response, containsString("richard.roe@baeldung.com"));
|
||||
assertThat(response, containsString("jane.doe@baeldung.com"));
|
||||
assertThat(response, containsString("phone"));
|
||||
assertThat(response, containsString("number"));
|
||||
assertThat(response, containsString("type"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue