こんにちは、放浪者です。今日はちょっとした魔法についてお話したいと思います。json についてはおそらくすでに聞いたことがあるでしょう。これは非常に普遍的な言語です。機械は理解でき、人間も簡単に読むことができます。json メッセージの典型的な例を次に示します。
{
"помещение":{
"название":"избушка",
"разумна":true
},
"основание":{
"тип":"курьи ноги",
"количество":2
},
"проживающие":[
{
"Name":"Баба Яга",
"профиль":"ведьма"
}
],
"местоположение":{
"address":"граница леса"
}
}
そうやってコミュニケーションをとるのは便利ですよね。これまで json が何なのか知らなかったとしても、今ではわかります。これを Java コードで使用するにはどうすればよいですか? Json はユニバーサル形式になりました。これは JavaScript Object Notation の略ですが、長い間 JavaScript を超えて、ほぼどこでも使用されています。Java には、json の操作を容易にするライブラリがいくつかあります。最も有名なのは次のとおりです。
2つ目を使います。codehausとfastxmlの 2 つのバージョンがありますが、違いはわかりませんでしたので、ここではどちらを使用しても構いません。コードの一部を次に示します。
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue("сюда json", "сюда класс");
json をオブジェクトに変換するのに役立ちます。そして私たちは最も重要なことに近づいています。この JSON のクラスを作成します。これは手動で行うことができ、次のような構造を作成します。
-----------------------------------com.fairytale.Base.java-----------------------------------
package com.fairytale;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"type",
"quantity"
})
public class Base {
@JsonProperty("type")
public String type = "";
@JsonProperty("quantity")
public int quantity = 0;
}
-----------------------------------com.fairytale.Hut.java-----------------------------------
package com.fairytale;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"room",
"base",
"residents",
"location"
})
public class Hut {
@JsonProperty("room")
public Room room;
@JsonProperty("base")
public Base base;
@JsonProperty("residents")
public List<Resident> residents = new ArrayList<Resident>();
@JsonProperty("location")
public Location location;
}
-----------------------------------com.fairytale.Location.java-----------------------------------
package com.fairytale;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"address"
})
public class Location {
@JsonProperty("address")
public String address = "";
}
-----------------------------------com.fairytale.Resident.java-----------------------------------
package com.fairytale;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name",
"profile"
})
public class Resident {
@JsonProperty("name")
public String name = "";
@JsonProperty("profile")
public String profile = "";
}
-----------------------------------com.fairytale.Room.java-----------------------------------
package com.fairytale;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name",
"reasonable"
})
public class Room {
@JsonProperty("name")
public String name = "";
@JsonProperty("reasonable")
public boolean reasonable = false;
}
特にゲッター、セッター、コンストラクター、その他の pojo 属性を省略しました。そうしないと無駄にうんざりするでしょう =) ここを見てください:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"room",
"base",
"residents",
"location"
],
"properties": {
"room": {
"$id": "#/properties/room",
"type": "object",
"title": "The Room Schema",
"required": [
"name",
"reasonable"
],
"properties": {
"name": {
"$id": "#/properties/room/properties/name",
"type": "string",
"title": "The Name Schema",
"default": "",
"examples": [
"избушка"
],
"pattern": "^(.*)$"
},
"reasonable": {
"$id": "#/properties/room/properties/reasonable",
"type": "boolean",
"title": "The Reasonable Schema",
"default": false,
"examples": [
true
]
}
},
"additionalProperties": false
},
"base": {
"$id": "#/properties/base",
"type": "object",
"title": "The Base Schema",
"required": [
"type",
"quantity"
],
"properties": {
"type": {
"$id": "#/properties/base/properties/type",
"type": "string",
"title": "The Type Schema",
"default": "",
"examples": [
"курьи ноги"
],
"pattern": "^(.*)$"
},
"quantity": {
"$id": "#/properties/base/properties/quantity",
"type": "integer",
"title": "The Quantity Schema",
"default": 0,
"examples": [
2
]
}
},
"additionalProperties": false
},
"residents": {
"$id": "#/properties/residents",
"type": "array",
"title": "The Residents Schema",
"items": {
"$id": "#/properties/residents/items",
"type": "object",
"title": "The Items Schema",
"required": [
"name",
"profile"
],
"properties": {
"name": {
"$id": "#/properties/residents/items/properties/name",
"type": "string",
"title": "The Name Schema",
"default": "",
"examples": [
"Баба Яга"
],
"pattern": "^(.*)$"
},
"profile": {
"$id": "#/properties/residents/items/properties/profile",
"type": "string",
"title": "The Profile Schema",
"default": "",
"examples": [
"ведьма"
],
"pattern": "^(.*)$"
}
},
"additionalProperties": false
}
},
"location": {
"$id": "#/properties/location",
"type": "object",
"title": "The Location Schema",
"required": [
"address"
],
"properties": {
"address": {
"$id": "#/properties/location/properties/address",
"type": "string",
"title": "The Address Schema",
"default": "",
"examples": [
"граница леса"
],
"pattern": "^(.*)$",
"additionalProperties": false
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}
これは上記の構造の json 図です。ここで、なぜそれが必要なのかを説明します。クラスを作成して維持する必要がなくなります。とても良いプロジェクトjsonschema2pojoがあります。ビルド時にこれらのクラスを作成するプロジェクト ビルダー (Maven、Gradle) 用のプラグインを提供します。私のプロジェクトの例を次に示します。
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.4.37</version>
<executions>
<execution>
<id>jsonschema2opjo</id>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/json-schema/</sourceDirectory>
<targetPackage>tester.model</targetPackage>
<outputDirectory>${project.basedir}/target/generated-sources/jsonschema/</outputDirectory>
<useCommonsLang3>true</useCommonsLang3>
<includeConstructors>true</includeConstructors>
<generateBuilders>true</generateBuilders>
<includeToString>true</includeToString>
<usePrimitives>true</usePrimitives>
</configuration>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
これが彼の設定です。最も興味深いのは次のとおりです。
<useCommonsLang3>true</useCommonsLang3>
<includeConstructors>true</includeConstructors>
<generateBuilders>true</generateBuilders>
<includeToString>true</includeToString>
<usePrimitives>true</usePrimitives>
これはクラスの書き方に関する説明です: useCommonsLang3 - CommonsLang3 ライブラリを使用します includeConstructors - コンストラクターを作成しますgenerateBuilders -パターン ビルダーを構築します includeToString - toString を追加します usePrimitives - プリミティブを使用するための命令 これが自作のコードよりも優れている点は何ですか?
-
クラスは 1 行でカスタマイズできます。たとえば、各クラスに Pojo サフィックスを追加する必要があります。<classNameSuffix>Pojo</classNameSuffix> を追加してプロジェクトを組み立てるだけで完了です。それ以外の場合は、各クラスの名前を手動で変更する必要があります。
これらのパラメーターはたくさんあります。ドキュメントですべてについて読む価値があります。
-
プロジェクトにコンシューマーがある場合は、Java クラスではなく json スキーマを与える方がはるかに簡単です。すでに述べたように、スキームは普遍的であり、消費者は自分の言語で pojo を生成するだけです。
-
それらははるかに小さいです。上記の例には、パターンや例など、必ずしも必要ではない情報が多数含まれています。しかし、それらを Java コードに戻すと、コードも大幅に増加します。テンプレート コードも忘れないでください。テンプレート コードは、プラグインのいくつかの設定によって図に示されていますが、コード内に自分で記述する必要があります。はい、ロンボク島については知っていますが、代替手段があります。
-
pojo にはロジックがありません。クラスが自分で作成されている場合、誰かが自分にとって便利なメソッドを追加したくなるかもしれません。このメソッドは、生成されたクラスだけでなく、json スキーマにも追加できません。
GO TO FULL VERSION