Search code examples
javapojoprotocol-buffers

How to create java pojo from the existing protobuf file?


I am trying to convert .proto (version 2.5) to java pojo. For example

From

message Article {
    optional string title = 1;
}

to

public class Article {
    private string title;

    public string getTitle() {
        return title;
    }

    public void setTitle(string title) {
        this.title = title;
    }
}

But the official protoc doesn't support generate pojo way java files.

Is there any tools that automatically generate pojo from .proto files?


Solution

  • This SO thread contains documentation for generating the Java "nano" variant of protocol buffers, which might just be what you are looking for: Android protobuf nano documentation

    Otherwise, the simplest way to generate code on your own is probably to use the google open source protocol buffer code generator to generate "descriptors" for your protocol buffers, and then iterate the descriptors (and perhaps use a library such as java poet) to generate Java code.