Skip to content
简体中文
English简体中文

安装指南 ​

SJF4J 支持 Java 8+。部分后端所依赖的第三方库可能要求更高的 Java 版本。

将示例中的 {version} 替换为 Maven Central 最新版本。

文档可能包含尚未正式发布的功能。请以对应依赖版本的 Javadoc 为准;最新 Javadoc 对应最新发布的 API。

快速配置 ​

安装 sjf4j-core,然后按需添加其他模块。以下使用 Jackson 2.x 作为 JSON 后端。

kotlin
dependencies {
    implementation("org.sjf4j:sjf4j-core:{version}")

    // Optional: Jackson 2.x backend (includes Jackson dependencies)
    implementation("org.sjf4j:sjf4j-backend-jackson2:{version}")

    // Optional: JSON Schema validation
    implementation("org.sjf4j:sjf4j-schema:{version}")

    // Optional: Compiled APIs
    annotationProcessor("org.sjf4j:sjf4j-processor:{version}")
}
xml
<dependencies>
    <dependency>
        <groupId>org.sjf4j</groupId>
        <artifactId>sjf4j-core</artifactId>
        <version>{version}</version>
    </dependency>

    <!-- Optional: Jackson 2.x backend (includes Jackson dependencies) -->
    <dependency>
        <groupId>org.sjf4j</groupId>
        <artifactId>sjf4j-backend-jackson2</artifactId>
        <version>{version}</version>
    </dependency>

    <!-- Optional: JSON Schema validation -->
    <dependency>
        <groupId>org.sjf4j</groupId>
        <artifactId>sjf4j-schema</artifactId>
        <version>{version}</version>
    </dependency>
</dependencies>

sjf4j-core 内置 Simple JSON 和 Java Properties 支持,使用这些格式时不需要外部后端。Maven 项目如需使用 Compiled API,还需单独配置注解处理器。

模块 ​

SJF4J 模块依赖关系

模块用途依赖
sjf4j-core结构化 API、数据绑定、导航、映射和补丁—
sjf4j-schemaJSON Schema 校验Core
sjf4j-processor编译时代码生成Core、Schema
sjf4j-spring将编译式接口注册为 Spring Bean(Java 17+)Core、Spring Framework
sjf4j-spring-boot-starterCompiled API 的 Spring Boot 自动配置(Java 17+)Spring、Spring Boot
sjf4j-backend-*JSON/YAML 后端适配器Core、对应的第三方库

按需引入模块即可。各后端适配器及 sjf4j-schema 均会传递引入 sjf4j-core。

后端 ​

SJF4J 内置及可选后端如下:

格式后端SJF4J 模块包含的第三方库
JSONJackson 3.xsjf4j-backend-jackson3tools.jackson.core:jackson-databind:3.1.8
JSONJackson 2.xsjf4j-backend-jackson2com.fasterxml.jackson.core:jackson-databind:2.22.3
JSONGsonsjf4j-backend-gsoncom.google.code.gson:gson:2.13.1
JSONFastjson2sjf4j-backend-fastjson2com.alibaba.fastjson2:fastjson2:2.0.66
JSONJSON-Psjf4j-backend-jsonpjakarta.json:jakarta.json-api:2.1.3,以及提供者 org.eclipse.parsson:parsson:1.1.7
JSONSimplesjf4j-core无
YAMLSnakeYAMLsjf4j-backend-snakeorg.yaml:snakeyaml:2.7
Properties内置sjf4j-core无

表中的版本为 SJF4J 项目当前使用的版本,其他兼容版本也可能正常工作。各后端模块均将对应的第三方库声明为 API 依赖(Gradle api、Maven compile scope),因此在编译期和运行时均可用,无需单独声明 Jackson、Gson、Fastjson2、JSON-P API 或 SnakeYAML 依赖。JSON-P 模块还会通过 runtimeOnly 默认引入 Parsson 作为运行时提供者。

JSON 后端自动选择优先级(由高到低):

Jackson 3 → Jackson 2 → Gson → Fastjson2 → JSON-P → Simple

SJF4J 按此顺序选择第一个可用后端。Runtime Binding 通过 ServiceLoader 发现提供者;@CompiledBinder(backend = Backend.AUTO) 在编译期选择后端。两者都要求相应适配器及原生库可用(JSON-P 还需要实现库)。Simple 是内置回退选项。

YAML 在 SnakeYAML 可用时使用它;Java Properties 无需额外依赖。选择规则及显式指定方法参阅 Backends & Formats。

Compiled APIs ​

将 sjf4j-processor 添加到注解处理器路径,而不是作为普通运行时依赖。

Gradle: 使用快速配置中的 annotationProcessor(...)。

Maven: 配置 Compiler Plugin:

xml
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.13.0</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.sjf4j</groupId>
                        <artifactId>sjf4j-processor</artifactId>
                        <version>{version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

@CompiledValidator 在运行时还需要 sjf4j-schema。其他 Compiled API 不使用 JSON Schema 功能时,无须引入该模块。

延伸阅读:Compiled APIs · Binding · Validating

Spring 集成 ​

以下两种方式均要求 Java 17+ 并启用注解处理。

Spring Framework ​

使用原生 Spring Framework 6.2+ 时,通过 @EnableSjf4j 启用编译式接口注册。

kotlin
implementation("org.sjf4j:sjf4j-spring:{version}")
annotationProcessor("org.sjf4j:sjf4j-processor:{version}")
xml
<dependency>
    <groupId>org.sjf4j</groupId>
    <artifactId>sjf4j-spring</artifactId>
    <version>{version}</version>
</dependency>
java
@Configuration
@EnableSjf4j
public class AppConfig {
}

Spring Boot ​

使用 Spring Boot 3.x 时,引入 Starter 即可自动启用相同的集成功能,无须添加 @EnableSjf4j。

kotlin
implementation("org.sjf4j:sjf4j-spring-boot-starter:{version}")
annotationProcessor("org.sjf4j:sjf4j-processor:{version}")
xml
<dependency>
    <groupId>org.sjf4j</groupId>
    <artifactId>sjf4j-spring-boot-starter</artifactId>
    <version>{version}</version>
</dependency>

两种方式都会传递引入 sjf4j-core。Maven 项目仍需单独配置注解处理器,其他可选 SJF4J 模块按需添加。

有关依赖注入、Spring Converter 和 JdbcTemplate 示例,参阅 Spring Integration。

基于 MIT 许可证发布。