You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用DSL JSON序列化反序列化时Java项目编译失败求助

问题描述

使用JDK 8开发Java项目,编译基于DSL JSON的序列化/反序列化代码时触发编译错误,错误提示Mono未检测到,无法搭建DSL-JSON处理环境。


相关代码与配置

ResponseBody实体类

public class ResponseBody {
    private String message;

    public ResponseBody(){
        this.message = null;
    }
    
    
    public ResponseBody(String message){
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

主方法代码

public static void main(String[] args) {
    try{
        DslJson dslJson = new DslJson<>();
        ResponseBody responseBody = new ResponseBody();
        responseBody.setMessage("hello saiful");

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        dslJson.serialize(responseBody, outputStream);
        byte[] serializedObject = outputStream.toByteArray();
        System.out.println("Object serialized to byte array");

        ByteArrayInputStream inputStream = new ByteArrayInputStream(serializedObject);
        responseBody = (ResponseBody) dslJson.deserialize(ResponseBody.class, inputStream);
        System.out.println("Object deserialized to byte array: "+responseBody.toString());

    }catch (Exception e){
        System.out.println(e);
    }
}

pom.xml依赖配置

<dependency>
    <groupId>com.dslplatform</groupId>
    <artifactId>dsl-json-processor</artifactId>
    <version>1.10.0</version>
</dependency>

编译错误信息

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Mono is required to run DSL compiler. Mono not detected or specified.
[ERROR] DSL compilation error
  Unable to setup DSL-JSON processing environment. Specify dsljson.loglevel=DEBUG for more information.

解决方案

1. 补全核心依赖

当前仅引入了注解处理器dsl-json-processor,缺少DSL JSON的核心运行库,需在pom.xml中添加:

<dependency>
    <groupId>com.dslplatform</groupId>
    <artifactId>dsl-json-java</artifactId>
    <version>1.10.0</version>
</dependency>

2. 消除Mono依赖要求

DSL JSON的注解处理器在JDK 8环境下默认依赖Mono,可通过以下两种方式解决:

  • 方式一:启用纯Java模式
    修改maven编译插件,添加编译参数指定使用纯Java模式,无需Mono:
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgs>
                        <arg>-Adsljson.usePureJava=true</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
  • 方式二:添加Mono依赖(编译阶段)
    如果必须保留默认模式,可添加Mono的provided依赖:
    <dependency>
        <groupId>org.mono</groupId>
        <artifactId>mono</artifactId>
        <version>1.0.0</version>
        <scope>provided</scope>
    </dependency>
    

3. 标记实体类为可序列化

给ResponseBody添加@CompiledJson注解,让DSL JSON处理器识别并生成序列化/反序列化代码:

import com.dslplatform.json.CompiledJson;

@CompiledJson
public class ResponseBody {
    // 原有代码不变
}

4. 优化反序列化输出

原代码中responseBody.toString()默认输出对象内存地址,建议重写该方法:

@Override
public String toString() {
    return "ResponseBody{message='" + message + "'}";
}

内容的提问来源于stack exchange,提问作者Md. Saiful Islam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 04:48:32