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

RESTful Web服务输出含多余$符号问题排查求助

Hey there! Let's dig into why those extra $ symbols are showing up in your RESTful Web Service output—this is a pretty common snag, usually tied to how your JSON serialization interacts with your POJO classes. Here are the most likely causes and fixes to try out:

Possible Causes & Fixes

1. Non-static inner classes in your POJO

If your POJO includes non-static inner classes, most JSON libraries (like Jackson) will serialize the hidden this$0 reference (a pointer to the outer class instance), which shows up as a $-prefixed field in your output.

How to fix it:

  • Convert the inner class to a static class, or
  • Move the inner class to its own top-level Java file.

Example correction:

// Before: Non-static inner class (causes $ fields)
public class OuterServiceClass {
    public class UserPOJO {
        private String username;
        // Getters & setters
    }
}

// After: Static inner class (eliminates $ fields)
public class OuterServiceClass {
    public static class UserPOJO {
        private String username;
        // Getters & setters
    }
}

2. JSON serializer including synthetic fields

Some serialization libraries automatically pick up synthetic fields—these are generated by the JVM for things like inner classes or lambda expressions, and they often have $ in their names.

Fix for Jackson (the most common JSON library for Spring/REST services):
Configure your ObjectMapper to ignore synthetic fields, either via code or annotations:

Option 1: Configure ObjectMapper directly

ObjectMapper mapper = new ObjectMapper();
// Add this rule to skip synthetic fields
mapper.configure(MapperFeature.IGNORE_SYNTHETIC_FIELDS, true);

// Optional: Add other standard configurations
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

Option 2: Use annotations on your POJO

@JsonIgnoreProperties(ignoreUnknown = true, value = {"this$0"})
public class YourPOJO {
    // Your fields, getters, and setters here
}

If you're using Lombok annotations like @Data or @Getter/@Setter, outdated versions or missing Jackson integration can lead to unexpected $ fields in serialization.

How to fix it:
First, ensure your pom.xml has the latest Lombok and Jackson-Lombok integration dependencies:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.30</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-lombok</artifactId>
    <version>2.15.2</version>
</dependency>

Then register the Lombok module with your ObjectMapper:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new LombokModule());

4. Unintended inherited or hidden fields

Double-check your POJO for any fields you didn't explicitly define—maybe inherited from a parent class—that have $ in their names. Use @JsonIgnore to exclude them from serialization.

Example:

public class YourPOJO {
    private String email;
    
    // Hidden field causing $ in output
    @JsonIgnore
    private String temp$SessionId;
    
    // Getters & setters
}
Quick Troubleshooting Tip

First, serialize your POJO directly in your service class (using ObjectMapper.writeValueAsString()) and print the result. This will confirm if the $ fields are coming from the POJO itself or from another part of your service logic—helping you narrow down the issue faster.

内容的提问来源于stack exchange,提问作者FDFDGSFG DJDBM

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:00:55