为什么在将 Yasson 与 JSON-B 一起使用时需要 Glassfish 的 javax.json 作为依赖项?
Why is javax.json from Glassfish needed as a dependency when using Yasson with JSON-B?
为了使用Java API for JSON Binding (JSON-B), I have found it necessary to include the following three dependencies in my Maven POM:
<!-- https://mvnrepository.com/artifact/jakarta.json.bind/jakarta.json.bind-api -->
<dependency>
<groupId>jakarta.json.bind</groupId>
<artifactId>jakarta.json.bind-api</artifactId>
<version>1.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse/yasson -->
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish/javax.json -->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>
前两个对我来说很有意义。
jakarta.json.bind-api
是 JSON-B API defined by JSR 367.
yasson
是那个API,Eclipse Yasson. 的参考实现
➥ 但是 Glassfish 中的第三个依赖项 javax.json
究竟给聚会带来了什么?为什么我的应用需要它才能运行?
如果省略,当运行 Jsonb jsonb = JsonbBuilder.create();
时,我得到这个错误:
javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found
我很困惑,因为我认为 Yasson 是 我的 JSON 处理实现。
实际上,您的代码应该只依赖于 api jakarta.json.bind-api
,因此您不会不小心使用 yasson
中的实现细节,例如内部 org.eclipse.yasson.internal.ReflectionUtils
。为此,您应该将 <scope>runtime</scope>
添加到 yasson
依赖项中。只是为了运行而已,你需要一个实现,而你选择了参考实现yasson
。
但是 JSON-B is just a layer on top of JSON-P:它执行绑定部分,同时将所有原始 JSON 处理委托给 JSON-P。您可以将任何 JSON-B 实现与任何 JSON-P 实现混合搭配。
因为 yasson
必须能够与任何 JSON-P 实现一起工作,所以它不能硬依赖于 Glassfish JSON-P 等;你必须自己指定它(也有 runtime
范围)。您看到的错误消息提到了 Glassfish,因为这是 JSON-P 正在寻找的后备实现。
为了使用Java API for JSON Binding (JSON-B), I have found it necessary to include the following three dependencies in my Maven POM:
<!-- https://mvnrepository.com/artifact/jakarta.json.bind/jakarta.json.bind-api -->
<dependency>
<groupId>jakarta.json.bind</groupId>
<artifactId>jakarta.json.bind-api</artifactId>
<version>1.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse/yasson -->
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish/javax.json -->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>
前两个对我来说很有意义。
jakarta.json.bind-api
是 JSON-B API defined by JSR 367.yasson
是那个API,Eclipse Yasson. 的参考实现
➥ 但是 Glassfish 中的第三个依赖项 javax.json
究竟给聚会带来了什么?为什么我的应用需要它才能运行?
如果省略,当运行 Jsonb jsonb = JsonbBuilder.create();
时,我得到这个错误:
javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found
我很困惑,因为我认为 Yasson 是 我的 JSON 处理实现。
实际上,您的代码应该只依赖于 api jakarta.json.bind-api
,因此您不会不小心使用 yasson
中的实现细节,例如内部 org.eclipse.yasson.internal.ReflectionUtils
。为此,您应该将 <scope>runtime</scope>
添加到 yasson
依赖项中。只是为了运行而已,你需要一个实现,而你选择了参考实现yasson
。
但是 JSON-B is just a layer on top of JSON-P:它执行绑定部分,同时将所有原始 JSON 处理委托给 JSON-P。您可以将任何 JSON-B 实现与任何 JSON-P 实现混合搭配。
因为 yasson
必须能够与任何 JSON-P 实现一起工作,所以它不能硬依赖于 Glassfish JSON-P 等;你必须自己指定它(也有 runtime
范围)。您看到的错误消息提到了 Glassfish,因为这是 JSON-P 正在寻找的后备实现。