Spring Boot请求体继承问题:@JsonSubTypes配置后解析报错求助
Alright, let's work through this problem together. I’ve dealt with similar headaches when combining JPA single-table inheritance with Jackson’s polymorphic deserialization, so here’s a breakdown of the most likely fixes:
1. Fix Your @JsonSubTypes Configuration
The error message Could not resolve type id 'com.nac.pool.select.MfiFilters' as a subtype tells me Jackson is trying to use the full class name as the type identifier, but your @JsonSubTypes setup doesn’t map that value to your MfiFilters class.
Two possible fixes here:
Option 1: Use a friendly type name (recommended)
Update your parentFiltersclass to explicitly map a short, readable type name to your subclass, and make sure your request uses that name. For example:@Entity @Table(name = "apply_filters", schema = "structure") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "filter_type") @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") @JsonSubTypes({ // Map the type name "mfi" to your MfiFilters class @JsonSubTypes.Type(name = "mfi", value = MfiFilters.class) }) public class Filters { // ... your entity fields and methods }Then in your request JSON, include the matching
typeproperty:{ "type": "mfi", // other fields specific to MfiFilters }Also, don’t forget to add the JPA discriminator value to your subclass to align with your database setup:
@Entity @DiscriminatorValue("mfi") // Matches the Jackson type name and database discriminator column public class MfiFilters extends Filters { // ... subclass fields }Option 2: Use full class names as type identifiers
If you want to keep using the full class name as thetypevalue in your request, change theuseattribute in@JsonTypeInfotoJsonTypeInfo.Id.CLASS:@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type")This way Jackson will directly use the fully qualified class name from the
typefield to resolve the subclass, no need to specify names in@JsonSubTypes(though it’s still okay to include them for clarity).
2. Verify Request JSON Format
Double-check that your incoming request actually includes the type property at the root level of the JSON object, and that its value exactly matches what you’ve configured (case-sensitive!). For example, if you mapped name = "mfi", sending "type": "Mfi" or "type": "MFIFilters" will cause the same resolution error.
3. Ensure No Conflicting Jackson Configurations
Make sure your Spring Boot project isn’t overriding the default Jackson polymorphic handling. If you have a custom ObjectMapper bean, verify that you haven’t disabled type information processing or modified the type resolver. For example, avoid code like this:
// This would break polymorphic deserialization objectMapper.disable(MapperFeature.USE_TYPE_ID_FOR_DESERIALIZATION);
4. Check Controller Method Signature
Confirm that your REST endpoint is accepting the parent Filters class as the request body, not the subclass. Your controller method should look like this:
@PostMapping("/apply-filters") public ResponseEntity<Filters> createFilter(@RequestBody Filters filter) { // Save or process the filter (Jackson will deserialize it to MfiFilters if type matches) filterRepository.save(filter); return ResponseEntity.ok(filter); }
9 times out of 10, the issue boils down to a mismatch between the type value in your request and the name mapped in @JsonSubTypes. Adjusting that alignment should resolve the deserialization error.
内容的提问来源于stack exchange,提问作者Sandy

