Here is my code:
I need to save java object value as jsonb in database (r2dbc).
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Table("scoring")
public class ScoringModel extends BaseModel {
@Column("client_id")
@SerializedName(value = "clientId", alternate = {"client_id"})
private String clientId;
//othres
@Column("languages")
@SerializedName(value = "languages", alternate = {"languages"})
private String languages;
@SerializedName(value = "response", alternate = {"response"})
//Need to save as JsonB
private Object response;
}
Please help me resolve the issue
2
Answers
You need to implement
ReadingConverter
andWritingConverter
and then register them inR2dbcCustomConversions
in configuration.Converters itself should produce JSONs.
If you using Postgres, there are two approaches you can use to map to Postgres JSON/JSONB fields.
Json
type directly in Java entity class.Json
via registering custom converters.The first one is simple and stupid.
Declare a
json
db type field, eg.Declare the
Json
type field in your entity class.For the second the solution, similarly
Declare
json
/jsonb
db type in the schema.sql.Declare the field type as your custom type.eg.
Declare a
R2dbcCustomConversions
bean.The example codes is here.
Finally verify it with a
@DataR2dbcTest
test.