怎么应用Protobuf 和Jersy 打造Rest Service

 我来答
18...6@163.com
2017-05-19 · TA获得超过436个赞
知道小有建树答主
回答量:1082
采纳率:0%
帮助的人:422万
展开全部
如何应用Protobuf 和Jersy 打造Rest Service

我使用的是Maven Project,所以
1。第一步加入Maven Dependency。

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.10</version>
<备誉/dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.4.1</version>
</dependency>

2。 然后,加入ant plugin来帮助我销坦们产生protobuf 的java bean。

<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</仿斗段phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<mkdir dir='target/generated-sources' />
<exec executable='protoc'>
<arg value='--java_out=target/generated-sources' />
<arg value='src/main/resources/addressbook.proto' />
</exec>
</tasks>
<sourceRoot>target/generated-sources</sourceRoot>
</configuration>

</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-antrun-plugin
</artifactId>
<versionRange>[1.3,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

3。 创建addressbook.proto文件

package tutorial;

option java_package = "com.sampullara.jaxrsprotobuf.tutorial";
option java_outer_classname = "AddressBookProtos";

message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;

enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}

message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}

repeated PhoneNumber phone = 4;
}

message AddressBook {
repeated Person person = 1;
}

4。 运行mvn generate-sources命令来生成AddressBookProtos.java文件。
5。创建rest server端代码AddressBookService.java
package com.sampullara;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.sampullara.jaxrsprotobuf.tutorial.AddressBookProtos;

@Path("/person")
public class AddressBookService {
@GET
@Produces("application/x-protobuf")
public AddressBookProtos.Person getPerson() {
return AddressBookProtos.Person.newBuilder()
.setId(1)
.setName("Sam")
.setEmail("sam@sampullara.com")
.addPhone(AddressBookProtos.Person.PhoneNumber.newBuilder()
.setNumber("415-555-1212")
.setType(AddressBookProtos.Person.PhoneType.MOBILE)
.build())
.build();
}

@POST
@Consumes("application/x-protobuf")
@Produces("application/x-protobuf")
public AddressBookProtos.Person reflect(AddressBookProtos.Person person) {
return person;
}
}

6。创建ProtobufProviders.java文件处理protobuf对象序列化。
package com.sampullara;

import com.google.protobuf.GeneratedMessage;
import com.google.protobuf.Message;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.WeakHashMap;
威孚半导体技术
2024-08-19 广告
威孚(苏州)半导体技术有限公司是一家专注生产、研发、销售晶圆传输设备整机模块(EFEM/SORTER)及核心零部件的高科技半导体公司。公司核心团队均拥有多年半导体行业从业经验,其中技术团队成员博士、硕士学历占比80%以上,依托丰富的软件底层... 点击进入详情页
本回答由威孚半导体技术提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式