国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

SpringBoot使用protobuf格式的接口方式

瀏覽:11日期:2023-02-21 10:59:55
SpringBoot使用protobuf格式的接口建立SpringBoot項目,pom.xml內容如下:

<?xml version='1.0' encoding='UTF-8'?><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 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example.protobuf</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version> </properties> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions><exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId></exclusion> </exclusions></dependency><dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.11.0</version></dependency><dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java-util</artifactId> <version>3.11.0</version></dependency><dependency> <groupId>com.googlecode.protobuf-java-format</groupId> <artifactId>protobuf-java-format</artifactId> <version>1.2</version></dependency><!-- 網絡請求依賴 --><dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version></dependency><dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.0</version></dependency><dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.0</version></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>編寫.proto文件,內容如下:

user_login.proto:

syntax = 'proto3';option java_package = 'com.boomsecret.protobuf';option java_outer_classname = 'MessageUserLogin';message MessageUserLoginRequest { string username = 1; string password = 2;}message MessageUserLoginResponse { string access_token = 1; string username = 2;}生成java代碼:

protoc.exe --java_out=./ user_login.proto將生成的代碼移動到你的項目中合適位置:

SpringBoot使用protobuf格式的接口方式

編寫protobuf格式的Controller接口:

package com.example.protobuf.demo.controller;import com.boomsecret.protobuf.MessageUserLogin;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpPost;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import util.HttpUtils;import java.net.URI;import java.util.UUID;@Controllerpublic class TestController { @RequestMapping(value = '/demo/test', produces = 'application/x-protobuf') @ResponseBody public MessageUserLogin.MessageUserLoginResponse getPersonProto(@RequestBody MessageUserLogin.MessageUserLoginRequest request) {MessageUserLogin.MessageUserLoginResponse.Builder builder = MessageUserLogin.MessageUserLoginResponse.newBuilder();builder.setAccessToken(UUID.randomUUID().toString()+'_res');builder.setUsername(request.getUsername()+'_res');return builder.build(); }}編寫測試類,通過HttpClient工具調用接口:

package com.example.protobuf.demo;import com.boomsecret.protobuf.MessageUserLogin;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpPost;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import util.HttpUtils;import java.net.URI;@SpringBootTestclass DemoApplicationTests { @Test void contextLoads() { } @Test public void test() {try { URI uri = new URI('http', null, '127.0.0.1', 8080, '/demo/test', '', null); HttpPost request = new HttpPost(uri); MessageUserLogin.MessageUserLoginRequest.Builder builder = MessageUserLogin.MessageUserLoginRequest.newBuilder(); builder.setUsername('tom'); builder.setPassword('123456'); HttpResponse response = HttpUtils.doPost(request, builder.build()); MessageUserLogin.MessageUserLoginResponse messageUserLoginResponse = MessageUserLogin.MessageUserLoginResponse.parseFrom(response.getEntity().getContent()); System.err.println(messageUserLoginResponse.getAccessToken());} catch (Exception e) {} }}HttpUtils內容如下:

package util;import com.google.protobuf.GeneratedMessageV3;import com.googlecode.protobuf.format.JsonFormat;import org.apache.http.Header;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.InputStreamEntity;import org.apache.http.impl.client.HttpClients;import java.io.ByteArrayInputStream;import java.io.IOException;/** * @author wangjinliang on 2018/10/18. */public class HttpUtils { public static HttpResponse doPost(HttpPost post, GeneratedMessageV3 message) throws IOException {HttpClient httpclient = HttpClients.createDefault();String requestUrl = post.getURI().toString();ByteArrayInputStream inputStream = new ByteArrayInputStream(message.toByteArray());InputStreamEntity inputStreamEntity = new InputStreamEntity(inputStream);post.setEntity(inputStreamEntity);post.addHeader('Content-Type', 'application/x-protobuf');for (Header header : post.getAllHeaders()) { System.out.println(header.getName() + ':' + header.getValue());}StringBuilder sb = new StringBuilder();sb.append('curl -XPOST ');for (Header header : post.getAllHeaders()) { sb.append(' -H '').append(header.getName()).append(':').append(header.getValue()).append(''');}String jsonBody = JsonFormat.printToString(message);jsonBody = jsonBody.replace(''', ''');sb.append(' -d '').append(jsonBody).append(''');sb.append(' ').append(requestUrl);System.out.println(sb.toString());return httpclient.execute(post); }}

以debug方式運行SpringBoot項目,并在controller加斷點,然后運行測試代碼:

SpringBoot使用protobuf格式的接口方式

可以看到請求過來的數據是正確的,放行后可以看到響應數據也是正確的:

SpringBoot使用protobuf格式的接口方式

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 日韩美女强理论片 | 国产成人午夜福在线观看 | 国产日韩三级 | 娇小性色xxxxx中文 | 亚洲图片视频在线观看 | 国产一区二区成人 | 韩国精品一区视频在线播放 | 久久久久18 | 亚洲精品人成网线在线 | 狠狠色丁香婷婷综合小时婷婷 | 久久怡红院国产精品 | 福利片免费一区二区三区 | 欧美成人h | 国产亚洲高清在线精品不卡 | aaaa欧美高清免费 | 男人天堂视频在线观看 | 在线观看日本免费视频大片一区 | 亚洲精品综合一二三区在线 | 欧美日韩一区二区视频免费看 | 在线观看成年人免费视频 | 欧美a免费 | 欧美中文一区 | 久久久国产99久久国产久 | 最新国产三级在线不卡视频 | 国产在线精品一区二区夜色 | 成人在线网 | 一本色道久久88加勒比—综合 | 毛片成人永久免费视频 | 成人三级毛片 | 国产2021中文天码字幕 | 久久国产精品亚洲 | 国产欧美日本亚洲精品五区 | 中文字幕在线一区二区在线 | 国产区二区 | 亚洲国产成人久久综合一 | 欧美日韩成人在线视频 | 精品精品国产自在久久高清 | 国产偷怕 | 国产精品怡红院在线观看 | 色秀视频在线观看88品善网 | 亚洲免费观看 |