API返回JSON仅映射3个字段至Java对象转换失败求助
Hey Arun, sorry to hear your WebClient JSON conversion is giving you trouble! Let's break down the most likely issues and fix them step by step—since your API returns 100+ fields but you only care about three, the problem is almost certainly related to how Jackson handles the extra fields or how your class is structured.
1. Ensure Your ArunApi Class Has Proper Access & Annotations
Jackson (which Spring WebClient uses under the hood) needs a way to set values on your private fields. The easiest fixes here are:
Option A: Use Lombok to Auto-Generate Getters/Setters
If you're using Lombok, add the @Data annotation to your class—it automatically generates getters, setters, and other utility methods Jackson needs:
import lombok.Data; @Data public class ArunApi { private String data; private Integer size; private String style; }
Option B: Manually Add Getters/Setters
If you don't use Lombok, explicitly define getters and setters for each field:
public class ArunApi { private String data; private Integer size; private String style; // Getters public String getData() { return data; } public Integer getSize() { return size; } public String getStyle() { return style; } // Setters public void setData(String data) { this.data = data; } public void setSize(Integer size) { this.size = size; } public void setStyle(String style) { this.style = style; } }
Bonus: Match JSON Field Names (If They Differ)
If the API's JSON fields use different naming (e.g., Data instead of data, Size instead of size), use @JsonProperty to map them:
import com.fasterxml.jackson.annotation.JsonProperty; public class ArunApi { @JsonProperty("Data") private String data; @JsonProperty("Size") private Integer size; @JsonProperty("Style") private String style; // Getters/Setters (or @Data if using Lombok) }
2. Configure Jackson to Ignore Unknown Fields
Since the API returns 100+ fields you don't care about, Jackson's default behavior is to throw an exception when it encounters fields your class doesn't define. Fix this by disabling the FAIL_ON_UNKNOWN_PROPERTIES feature in your WebClient's ObjectMapper:
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.http.codec.json.Jackson2JsonDecoder; import org.springframework.http.codec.json.Jackson2JsonEncoder; import org.springframework.web.reactive.function.client.WebClient; // Create a custom ObjectMapper that ignores unknown fields ObjectMapper objectMapper = new ObjectMapper() .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // Build WebClient with this custom ObjectMapper WebClient webClient = WebClient.builder() .codecs(configurer -> configurer.defaultCodecs() .jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper)) .jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper))) .build();
This tells Jackson to ignore any fields in the JSON that aren't defined in your ArunApi class.
3. Verify JSON Field Types Match Your Class
Double-check that the API's JSON values match your field types:
- If the
sizefield in the JSON is a string (e.g.,"size": "25"instead of"size": 25), converting toIntegerwill fail. Fix this either by:- Asking the API to return a numeric
size, or - Using
@JsonDeserializeto convert the string to an integer:import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.deser.std.NumberDeserializers; public class ArunApi { private String data; @JsonDeserialize(using = NumberDeserializers.IntegerDeserializer.class) private Integer size; private String style; // Getters/Setters }
- Asking the API to return a numeric
4. Debug the Exact Issue
If you're still stuck, first retrieve the raw JSON response to inspect it:
String rawJson = webClient.get() .uri(endPoint) .retrieve() .bodyToMono(String.class) .block(); System.out.println("Raw JSON Response: " + rawJson);
Then try manually converting this string to your ArunApi class to see the exact exception:
ArunApi testConversion = objectMapper.readValue(rawJson, ArunApi.class);
The exception message will tell you exactly what's wrong (e.g., missing field, type mismatch, unknown property).
内容的提问来源于stack exchange,提问作者Arun

