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

预定义POJO中添加属性可变对象,用JsonObject调API遇404求助

Fixing Dynamic 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 the Map
  • 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 (only id, 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 JsonObject type 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:24:49