我这里是以pb2为例,2和3不兼容,但是方法大致相同
1 pom依赖
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.protobuf-java-format</groupId>
<artifactId>protobuf-java-format</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.protobuf-java-format</groupId>
<artifactId>protobuf-java-format</artifactId>
<version>1.4</version>
</dependency>
<dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>com.googlecode.protobuf-java-format</groupId> <artifactId>protobuf-java-format</artifactId> <version>1.4</version> </dependency>
2 编译pb文件
编译器在这里下载:https://repo1.maven.org/maven2/com/google/protobuf/protoc/2.6.1/
protoc --java_out=./path/pb ./xxx.proto
protoc --java_out=./path/pb ./xxx.proto
protoc --java_out=./path/pb ./xxx.proto
3 增加Converter
@Configuration
public class ProtobufHttpMessageConverterConfiguration {
@Bean
public ProtobufHttpMessageConverter protobufHttpMessageConverter() {
return new ProtobufHttpMessageConverter();
}
@Bean
public RestTemplate restTemplate(ProtobufHttpMessageConverter protobufHttpMessageConverter) {
List<HttpMessageConverter<?>> converterList = XXX // 此处是你要兼容的其他自定义的converter,比如json之类的,可以提前创建好
converterList.add(protobufHttpMessageConverter);
return new RestTemplate(converterList);
}
}
@Configuration
public class ProtobufHttpMessageConverterConfiguration {
@Bean
public ProtobufHttpMessageConverter protobufHttpMessageConverter() {
return new ProtobufHttpMessageConverter();
}
@Bean
public RestTemplate restTemplate(ProtobufHttpMessageConverter protobufHttpMessageConverter) {
List<HttpMessageConverter<?>> converterList = XXX // 此处是你要兼容的其他自定义的converter,比如json之类的,可以提前创建好
converterList.add(protobufHttpMessageConverter);
return new RestTemplate(converterList);
}
}
@Configuration public class ProtobufHttpMessageConverterConfiguration { @Bean public ProtobufHttpMessageConverter protobufHttpMessageConverter() { return new ProtobufHttpMessageConverter(); } @Bean public RestTemplate restTemplate(ProtobufHttpMessageConverter protobufHttpMessageConverter) { List<HttpMessageConverter<?>> converterList = XXX // 此处是你要兼容的其他自定义的converter,比如json之类的,可以提前创建好 converterList.add(protobufHttpMessageConverter); return new RestTemplate(converterList); } }
4 Controller添加
@RestController
@RequestMapping(value = "/xxx")
public class TencentRtaController {
@PostMapping(produces = "application/x-protobuf")
public @ResponseBody
XXXProtos.XXResponse rta(@RequestBody XXXProtos.XXRequest request) throws Exception {
// TODO Remove Mock
XXXProtos.XXResponse.Builder builder = XXXProtos.XXResponse.newBuilder();
builder.setResult(200);
return builder.build();
}
}
@RestController
@RequestMapping(value = "/xxx")
public class TencentRtaController {
@PostMapping(produces = "application/x-protobuf")
public @ResponseBody
XXXProtos.XXResponse rta(@RequestBody XXXProtos.XXRequest request) throws Exception {
// TODO Remove Mock
XXXProtos.XXResponse.Builder builder = XXXProtos.XXResponse.newBuilder();
builder.setResult(200);
return builder.build();
}
}
@RestController @RequestMapping(value = "/xxx") public class TencentRtaController { @PostMapping(produces = "application/x-protobuf") public @ResponseBody XXXProtos.XXResponse rta(@RequestBody XXXProtos.XXRequest request) throws Exception { // TODO Remove Mock XXXProtos.XXResponse.Builder builder = XXXProtos.XXResponse.newBuilder(); builder.setResult(200); return builder.build(); } }