预定义POJO中添加属性可变对象,用JsonObject调API遇404求助
platformSpecificPreferences in Your REST API POJO Hey there! Let's work through this problem with your dynamic platformSpecificPreferences field in your REST API POJO, and figure out why you're hitting that 404 error.
First, a quick note: 404 usually means the server can't find your endpoint, but since this started happening right after you added the JsonObject field, it's almost certainly that the request body is failing to deserialize, and your framework is returning a misleading 404 instead of a 400 Bad Request. Let's fix the dynamic field handling first, then confirm the endpoint is working.
Solution 1: Use Map<String, Object> Instead of JsonObject
Most JSON processing libraries (like Jackson, the standard in Spring Boot) handle Map types seamlessly for dynamic key-value structures. Replace your JsonObject field with a Map and keep the @JsonProperty annotation—this is the simplest, most reliable approach:
public class YourRequestPayload { private String id; private boolean enableTouchId; private int idleLogoutMinutes; @JsonProperty("platformSpecificPreferences") private Map<String, Object> platformSpecificPreferences; // Add getters and setters for all fields }
This works because:
- Jackson automatically serializes/deserializes the nested dynamic objects (like
ios,web,android) into theMap - No extra dependencies or complex configuration needed—this is a native, well-supported pattern for dynamic JSON structures
Solution 2: Use @JsonAnySetter and @JsonAnyGetter (Alternative)
If you prefer a more encapsulated approach, you can use Jackson's @JsonAnySetter and @JsonAnyGetter annotations to wire dynamic properties into a hidden Map:
public class YourRequestPayload { private String id; private boolean enableTouchId; private int idleLogoutMinutes; private final Map<String, Object> platformSpecificPreferences = new HashMap<>(); // Getters and setters for fixed fields @JsonAnySetter public void addPlatformPreference(String key, Object value) { platformSpecificPreferences.put(key, value); } @JsonAnyGetter public Map<String, Object> getPlatformSpecificPreferences() { return platformSpecificPreferences; } }
This achieves the same result as the first solution but gives you more control over how dynamic properties are stored and accessed.
Troubleshooting the 404 Error
Once you've updated your POJO:
- Test with a simplified request: Send a request without
platformSpecificPreferences(onlyid,enableTouchId,idleLogoutMinutes) to confirm the endpoint itself is reachable. If this returns a 200, the original 404 was definitely caused by deserialization failure. - Double-check your request details: Verify you're using the correct URL path (e.g.,
POST /api/user-preferences) and HTTP method that matches your controller's mapping. - Check server logs: Look for deserialization errors in your framework logs—they'll confirm that the
JsonObjecttype was causing parsing issues.
Why JsonObject Caused Issues
JsonObject (from libraries like Gson or Jakarta EE) isn't a standard type that Jackson knows how to deserialize by default. You'd need extra configuration or dependencies to make it work, which is unnecessary when Map is a simpler, out-of-the-box solution.
内容的提问来源于stack exchange,提问作者Nutan

