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:
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
staticclass, 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 }
3. Lombok-related field generation (if you're using Lombok)
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 }
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

