私は私のモデルランニングのための私のフォーマッタを取得しようとしています。このモデルは、次のコードのような注釈が含まれています。私は私がまだ動作してませんが、問題を把握することはできません複数のフォーマッタを持っています。
public class Customer {
@Trim
private String firstName;
//some other properties, getter and setter
}
注釈は正しく私の知る限りでは、設定されています:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
public @interface Trim {
boolean squashMultipleSpaces() default true;
}
そしてAnnotationFormatterFactory
public class TrimAnnotationFormatterFactory implements AnnotationFormatterFactory<Trim> {
public TrimAnnotationFormatterFactory() {
}
public Set<Class<?>> getFieldTypes() {
return Collections.singleton(String.class);
}
public Printer<String> getPrinter(Trim annotation, Class<?> fieldType) {
return new TrimAnnotationFormatterFactory.TrimFormatter(annotation.squashMultipleSpaces());
}
public Parser<String> getParser(Trim annotation, Class<?> fieldType) {
return new TrimAnnotationFormatterFactory.TrimFormatter(annotation.squashMultipleSpaces());
}
private static class TrimFormatter implements Formatter<String> {
private final boolean squashMultipleSpaces;
TrimFormatter(boolean squashMultipleSpaces) {
this.squashMultipleSpaces = squashMultipleSpaces;
}
public String parse(String text, Locale locale) {
return this.process(text);
}
public String print(String object, Locale locale) {
return this.process(object);
}
private String process(String text) {
if (text == null) {
return null;
} else {
return this.squashMultipleSpaces ? text.trim().replaceAll("\\s+", " ") : text.trim();
}
}
}
}
私はFormatterRegistryに私のカスタムAnnotationFormatterFactoryを追加しました。私はそれをdbuggignだとき、私はそれが正常にFormatterRegistryに追加されていることがわかります。
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldAnnotation(new TrimAnnotationFormatterFactory());
}
}
そして、コントローラのルックスが好き:
@Controller
public class CustomerController {
@PostMapping(value = "/customer")
@ResponseBody
public Customer saveCustomer(@RequestBody Customer customer) {
return customer;
}
}
私の入力は次のように見える場合
" Christian Walter"
コントローラにモデルはまだ同じです。私は期待していました
"Christian Walter"
私のモデルインチ
なぜ私のフォーマッタの仕事ではないのですか?それとも私がPropertyEditorはを使用する必要がないのであれば、どのように私は、注釈でそれを使うだろうか?
UPDATE:フォーマッタが正常に登録されていますが、呼び出されません。そして、コントローラを追加しました。
ご協力いただきありがとうございます。
あなたの最後の更新からは、私はあなたがあなたのオブジェクトをデシリアライズするためにJSONを使用していることを理解しています。この場合、あなたは(私の意見では)として、独自のデシリアライザを記述する必要があります。
public class WhiteSpaceTrimmerDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
JsonNode node = jp.getCodec().readTree(jp);
return node.asText().replaceAll("\\s+", " ").trim();
}
}
そして、同じように使用:
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
public class Customer {
private String firstName;
public String getFirstName() {
return firstName;
}
@JsonDeserialize(using = WhiteSpaceTrimmerDeserializer.class)
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
だから、" Christian Walter"
生成されます"Christian Walter"
。
あなたが見ることができ、このデシリアライザを設定するための別の方法があります:缶ジャクソンは、すべての文字列プロパティから先頭/末尾の空白をトリムするように構成することが?
しかし、なぜFormatter
動作しませんか?私が持っている唯一の説明は、ある@RequestBody/@ResponseBody
では処理されませんWebMvcConfigurer
。