Loading Protobuf to Java Converter...
Please wait a moment

How to Turn a .proto File into Java Classes

A lot of Java code never touches the Protocol Buffers binary format: it receives the JSON version of a message from a gateway, a log or another team's REST endpoint. For that, a set of small Jackson-friendly classes is often easier to live with than the full protobuf-java runtime. This page reads your .proto file and writes those classes, with field names and JSON names that match what protobuf itself uses.

Step 1

Paste or upload the .proto file

Paste a schema into the left editor, upload a .proto file (up to 10 MB), or press Sample. proto2, proto3 and edition files are read, and a snippet without a syntax line is treated as proto3.

Example: a small order schema

syntax = "proto3";

package shop.v1;

option java_package = "com.example.shop";
option java_outer_classname = "ShopModels";

message Order {
  int64 id = 1;
  string customer_id = 2;
  repeated Item items = 3;
  optional string coupon = 4;
  Status status = 5;
}

message Item {
  string sku = 1;
  uint32 qty = 2;
  double price = 3;
}

enum Status {
  STATUS_UNSPECIFIED = 0;
  STATUS_PAID = 1;
}
Step 2

Look over the Java classes

Every message becomes a static class with private fields, getters and setters, nested the same way as in the .proto file, and every enum becomes a Java enum. Anything you should know about, such as a service that was skipped, a renamed field or a type from a file you did not paste, is listed above the code. A mistake in the .proto file shows its line and column instead.

What the page writes for that schema

customer_id becomes customerId (with @JsonAlias so the snake_case name is read too), the optional coupon is a String that can stay null, qty is a long because uint32 can go past Integer.MAX_VALUE, and the package and outer class come from the java_package and java_outer_classname options.

// Plain Java classes for the messages in this .proto file, with Jackson annotations that
// follow protobuf's JSON field names. They read and write JSON (for example JSON from
// protobuf-java's JsonFormat or a REST API). They are not protobuf-java messages: for the
// binary format and gRPC, generate code with protoc --java_out.
//
// Needs Jackson: com.fasterxml.jackson.core:jackson-databind
// Read JSON with: ShopModels.Order value = new ObjectMapper().readValue(json, ShopModels.Order.class);
//
// Note: int64 fields are Java long, so long IDs such as 8901240544102066246 keep every digit. protobuf's JSON writer puts 64-bit numbers in quotes; Jackson reads those quoted numbers into long fields as they are.
//
// Note: Unsigned fields use the next size up so every value fits: uint32/fixed32 → long, uint64/fixed64 → BigInteger. (protobuf-java itself keeps them in int/long and reuses the sign bit.)
//
// Note: Enums read and write their value names in JSON, like protobuf does, and also accept the numbers.

package com.example.shop;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.List;

public final class ShopModels {
    private ShopModels() {
    }

    /** The message shop.v1.Order. */
    @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
            isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
    public static class Order {
        @JsonProperty("id")
        private long id;
        @JsonProperty("customerId")
        @JsonAlias("customer_id")
        private String customerId;
        @JsonProperty("items")
        private List<Item> items;
        @JsonProperty("coupon")
        private String coupon;
        @JsonProperty("status")
        private Status status;

        public long getId() { return id; }
        public void setId(long id) { this.id = id; }
        public String getCustomerId() { return customerId; }
        public void setCustomerId(String customerId) { this.customerId = customerId; }
        public List<Item> getItems() { return items; }
        public void setItems(List<Item> items) { this.items = items; }
        public String getCoupon() { return coupon; }
        public void setCoupon(String coupon) { this.coupon = coupon; }
        public Status getStatus() { return status; }
        public void setStatus(Status status) { this.status = status; }
    }

    /** The message shop.v1.Item. */
    @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
            isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
    public static class Item {
        @JsonProperty("sku")
        private String sku;
        @JsonProperty("qty")
        private long qty;
        @JsonProperty("price")
        private double price;

        public String getSku() { return sku; }
        public void setSku(String sku) { this.sku = sku; }
        public long getQty() { return qty; }
        public void setQty(long qty) { this.qty = qty; }
        public double getPrice() { return price; }
        public void setPrice(double price) { this.price = price; }
    }

    /** The enum shop.v1.Status. JSON uses the value names. */
    public enum Status {
        STATUS_UNSPECIFIED("STATUS_UNSPECIFIED", 0),
        STATUS_PAID("STATUS_PAID", 1);

        private final String protoName;
        private final int protoNumber;

        Status(String protoName, int protoNumber) {
            this.protoName = protoName;
            this.protoNumber = protoNumber;
        }

        /** The number from the .proto file. */
        public int getNumber() {
            return protoNumber;
        }

        /** Written to JSON as the value name, the way protobuf's JSON mapping does. */
        @JsonValue
        public String getProtoName() {
            return protoName;
        }

        /** Reads the value name or its number. */
        @JsonCreator
        public static Status fromJson(Object value) {
            for (Status v : values()) {
                if (value instanceof Number ? ((Number) value).longValue() == v.protoNumber : v.protoName.equals(String.valueOf(value))) {
                    return v;
                }
            }
            throw new IllegalArgumentException("Unknown Status value: " + value);
        }
    }
}
Step 3

Copy or download the file

Copy the code or download it as a .java file named after the outer class. The only library it needs is Jackson (jackson-databind).

<!-- pom.xml -->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.17.2</version>
</dependency>

// reading the JSON form of an Order
ObjectMapper mapper = new ObjectMapper();
ShopModels.Order order = mapper.readValue(json, ShopModels.Order.class);
System.out.println(order.getId() + " " + order.getStatus());

When you need the binary format or gRPC, generate protobuf-java code with protoc --java_out instead. To check a schema first, use the Proto File Validator, and if all you have is a JSON sample, JSON to Java builds classes from the data itself.

Frequently Asked Questions

Is this what protoc --java_out generates?

No. protoc writes protobuf-java message classes with builders, binary parsing and descriptors, and gRPC stubs come from a separate plugin. This page writes plain Java classes that Jackson can fill from JSON, which is handy when a service hands you the JSON form of a message (for example from JsonFormat.printer() or a REST gateway) and you only want typed objects. Like protoc, it wraps everything in one outer class, named after java_outer_classname when the file sets it.

Which Java type does each .proto type get?

int32, sint32 and sfixed32 become int; int64, sint64 and sfixed64 become long; uint32 and fixed32 become long and uint64 and fixed64 become BigInteger, so the largest unsigned values still fit. float, double, bool and string map straight across and bytes becomes byte[] (Base64 in JSON). repeated fields are List, map<K, V> fields are Map with boxed keys, other messages are their class, and enums are Java enums. google.protobuf.Timestamp and Duration stay strings, Struct becomes Map<String, Object> and the wrapper types become boxed values such as Long.

Do long 64-bit IDs keep all their digits?

Yes. An int64 field is a Java long, so 8901240544102066246 is read exactly, and a uint64 field holds anything up to 18446744073709551615. protobuf's JSON writer puts 64-bit numbers in quotes, and Jackson reads those quoted numbers into long and BigInteger fields without any extra setup.

How do optional fields, oneofs and enums come out?

Plain proto3 fields use primitive types, so a missing value reads as 0 or false. Fields marked optional, every optional field in proto2 and the members of a oneof use boxed types (Integer, Long, Boolean) that stay null when the JSON leaves them out; oneof members sit under a comment naming the oneof. Enums write their value names to JSON and read either the name or the number, the way protobuf's JSON mapping does.

What if a name clashes with Java?

Field names are camelCased the protoc way (customer_id becomes customerId), and a field called class, int or default gets a trailing underscore. A message named String, List or Map is renamed (StringMessage and so on) so it does not hide the Java type. The JSON name never changes: each field carries a @JsonProperty with protobuf's JSON name, plus a @JsonAlias with the original field name when the two differ. Every rename is listed in a note above the code.

Is my .proto file uploaded anywhere?

No. The conversion runs in your browser and the file is not sent to our server. Nothing is kept either: reload the page and the editor starts empty.