Converts JSON documents into a Spring Data MongoDB @Document class: the collection named in @Document, an @Id field, a private field per key with @Field where the stored key differs, plus Lombok and jakarta.validation annotations, all in one Java file.
Use it when data that lands in MongoDB as plain JSON (IoT readings, events, scraped results) needs to be read and written from a Spring Boot service through a MongoRepository.
find().limit(5) in mongosh, separates occasional fields from constant ones.Model name (empty means MyModel); it is used for both the class name and the collection in @Document(collection = "...").package line, save the file, and create a repository extending MongoRepository<DeviceLog, String>.[
{ "device_id": "th-sensor-07", "temperature": 21.4, "battery": 87, "online": true, "recorded_at": "2024-02-11T06:00:00Z" },
{ "device_id": "th-sensor-09", "temperature": 19.8, "battery": 12, "online": false, "recorded_at": "2024-02-11T06:00:00Z", "firmware": "1.4.2" }
]package com.hello.world.devicelog;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldNameConstants;
@Document(collection = "DeviceLog")
@FieldNameConstants
@Getter
@Setter
public class DeviceLog {
@Id
private String id;
@NotEmpty()
@Field("device_id")
private String deviceId;
@NotNull()
private Double temperature;
@NotNull()
private Long battery;
@NotNull()
private Boolean online;
@NotNull()
@Field("recorded_at")
private LocalDateTime recordedAt;
private String firmware;
}firmware appears only in the second document, so it has no annotation. device_id and recorded_at are read through @Field("device_id") and @Field("recorded_at"), so existing documents map onto the camelCase fields without migration.
@Document(collection = "DeviceLog").@Id private String id holds MongoDB's _id; an id or _id key in your JSON is folded into it rather than repeated, and a numeric id makes it Long.Long, Double, Boolean, LocalDate (YYYY-MM-DD), LocalDateTime (date-times) or String; embedded objects become Map<String, Object> and arrays List<...>.@Field("...") is added whenever the Java name is not the stored key, including Java keywords renamed with a trailing _.@NotEmpty() for non-empty strings and @NotNull() otherwise; occasional or null fields get none.When readings that devices write straight into MongoDB must be served to a dashboard, paste a few documents to get the class and immediately declare derived queries such as findByDeviceIdOrderByRecordedAtDesc.
If Spring needs to read a collection another service already writes in snake_case, the @Field annotations let the Java side use camelCase names while reading and writing the same keys.
@NotEmpty and @NotNull are not checked on save by default; register a ValidatingMongoEventListener bean to validate right before save().LocalDateTime is stored as a UTC-based Date in MongoDB and can appear shifted depending on the server time zone. For logs where the exact instant matters, Instant is safer.Long id is not generated by MongoDB; you must assign it before saving. Only String, ObjectId and BigInteger ids are filled in automatically.Map<String, Object>. Replace them with small classes when you want typed access and validation.id when MongoDB stores _id?Spring Data maps a field named id (or annotated @Id) to _id automatically and converts a String to an ObjectId when it looks like one. Declaring _id yourself is unnecessary.
device_logs.Just edit the string: @Document(collection = "device_logs"). The generator inserts the model name as typed, so entering device_logs as the model name would also turn the class into DeviceLogs.
It uses @Document, @Field and Spring Data's @Id instead of @Entity, @Column and @GeneratedValue, and nested values stay embedded instead of needing a JSON column. For relational tables, use the JSON → Spring Boot JPA page.