{"record":{"id":"59f2420fc87062b2","repo":"code4craft/webmagic","slug":"can-t-find-formatter-for-field-of-type","errorCode":null,"errorMessage":"Can't find formatter for field  of type ","messagePattern":"Can't find formatter for field  of type ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"webmagic-extension/src/main/java/us/codecraft/webmagic/model/formatter/ObjectFormatterBuilder.java","lineNumber":28,"sourceCode":" * @since 0.7.0\n *         Date: 2017/6/3\n */\npublic class ObjectFormatterBuilder {\n\n    private Field field;\n\n    public ObjectFormatterBuilder setField(Field field) {\n        this.field = field;\n        return this;\n    }\n\n    private ObjectFormatter initFormatterForType(Class<?> fieldClazz, String[] params) {\n        if (fieldClazz.equals(String.class) || List.class.isAssignableFrom(fieldClazz)){\n            return null;\n        }\n        Class<? extends ObjectFormatter> formatterClass = ObjectFormatters.get(BasicTypeFormatter.detectBasicClass(fieldClazz));\n        if (formatterClass == null) {\n            throw new IllegalStateException(\"Can't find formatter for field \" + field.getName() + \" of type \" + fieldClazz);\n        }\n        return initFormatter(formatterClass, params);\n    }\n\n    private ObjectFormatter initFormatter(Class<? extends ObjectFormatter> formatterClazz, String[] params) {\n        try {\n            ObjectFormatter objectFormatter = formatterClazz.newInstance();\n            objectFormatter.initParam(params);\n            return objectFormatter;\n        } catch (InstantiationException e) {\n            throw new RuntimeException(e);\n        } catch (IllegalAccessException e) {\n            throw new RuntimeException(e);\n        }\n    }\n\n    public ObjectFormatter build() {\n        Formatter formatter = field.getAnnotation(Formatter.class);","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/code4craft/webmagic/blob/67816a19d68a4fec4657bf1336227e046e251df2/webmagic-extension/src/main/java/us/codecraft/webmagic/model/formatter/ObjectFormatterBuilder.java#L10-L46","documentation":"ObjectFormatterBuilder.initFormatterForType looks up a registered ObjectFormatter for the field's basic type. If the field type is not String, not a List, and has no registered formatter for its detected basic class, IllegalStateException is thrown — the model framework cannot convert extracted strings into that field type. (The message text omits field name/type interpolation in this version.)","triggerScenarios":"Declaring a model field of an unsupported type such as Date with an unregistered format, custom POJO, Integer variant without a formatter, then running a page-model spider so build()/initFormatterForType executes.","commonSituations":"Model beans with fields typed Long/Double/Boolean beyond registered formatters, custom enum or POJO fields, or Date fields after the date-format handling changed between WebMagic versions.","solutions":["Change the field type to String (or List<String>) and convert manually afterwards","Register a custom ObjectFormatter for the type via ObjectFormatters before creating the extractor","Use a supported basic type (e.g. int/long/boolean wrappers that have formatters) or add a getter-based conversion"],"exampleFix":"// before\nprivate MyPojo detail;\n// after\nprivate String detail; // convert to MyPojo after extraction","handlingStrategy":"validation","validationCode":"Class<?> t = field.getType(); if (!String.class.equals(t) && !List.class.isAssignableFrom(t) && ObjectFormatters.get(BasicTypeFormatter.detectBasicClass(t)) == null) { /* unsupported field type */ }","typeGuard":"boolean hasFormatter(Class<?> t) { return String.class.equals(t) || List.class.isAssignableFrom(t) || ObjectFormatters.get(BasicTypeFormatter.detectBasicClass(t)) != null; }","tryCatchPattern":"try { extractor = PageModelExtractor.create(Model.class); } catch (IllegalStateException e) { log.error(\"no formatter for a model field type\", e); }","preventionTips":["Prefer String/List<String> fields in page models","Register custom ObjectFormatters for POJO types before create()","Keep model field types to basic wrappers with built-in formatters"],"tags":["webmagic","model","formatter","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"67816a19d68a4fec4657bf1336227e046e251df2","analyzedAt":"2026-09-08T11:15:28.425Z","contentChangedAt":"2026-09-08T11:15:28.425Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}