Spring REST分页排序过滤接口JSON解析异常处理:移除字段值末尾反斜杠避免抛出异常
Hey there, let's work through this JSON parsing issue you're hitting with your Spring REST API. The problem pops up when your name filter value ends with a backslash (\)—Jackson's JSON parser treats this as an incomplete escape sequence, throwing an error. And as you saw, enabling spring.jackson.parser.allow-unquoted-control-chars=true just introduces new parsing problems because it relaxes JSON validation too much.
Root Cause
When your request JSON includes "name": "user_2\\", the trailing backslash is interpreted as the start of an escape character (like \" for a quote), but there's no character after it to escape. This breaks standard JSON syntax, hence the initial parsing exception. The global config tweak you tried doesn't fix the root issue—it just allows other invalid characters, leading to new structural errors.
Solution: Automatically Remove Trailing Backslashes During Deserialization
The cleanest approach is to handle the trailing backslash directly in your DTO, either via a custom Jackson deserializer or a modified setter method. Both methods will clean the input before it causes parsing issues.
Option 1: Custom Jackson Deserializer
Create a custom deserializer that checks for and removes trailing backslashes from string values:
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import java.io.IOException; public class TrailingBackslashCleaner extends StdDeserializer<String> { public TrailingBackslashCleaner() { super(String.class); } @Override public String deserialize(JsonParser parser, DeserializationContext context) throws IOException { String rawValue = parser.getValueAsString(); if (rawValue != null && rawValue.endsWith("\\")) { // Trim the trailing backslash return rawValue.substring(0, rawValue.length() - 1); } return rawValue; } }
Then apply this deserializer to your UserFilterDTO's name field:
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class UserFilterDTO { @JsonDeserialize(using = TrailingBackslashCleaner.class) private String name; }
Option 2: Modified Setter Method (Simpler Approach)
If you don't want to create a separate deserializer, you can modify the setter for the name field to clean the input:
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class UserFilterDTO { private String name; public void setName(String name) { if (name != null && name.endsWith("\\")) { this.name = name.substring(0, name.length() - 1); } else { this.name = name; } } }
Why This Works
Both approaches intercept the input string before it can cause parsing errors. The deserializer runs during Jackson's parsing process, cleaning the value as it's read. The setter method runs right after parsing, ensuring the stored value doesn't have trailing backslashes.
Testing the Fix
Send your sample request JSON:
{ "pageSize":5, "pageIndex":0, "filter": { "name": "user_2\\" } }
The name value will be automatically converted to user_2, and the JSON parsing will complete without exceptions—no global config changes needed.
Avoid Global Jackson Config Changes
Steer clear of allow-unquoted-control-chars=true because it weakens JSON validation, allowing invalid control characters that can break other parts of your API's JSON parsing (like the second exception you encountered). Handling the issue at the field level is safer and more targeted.
内容的提问来源于stack exchange,提问作者Zero Hero

