我正在使用spring框架处理rest api。 我尝试使用spring-boot的自定义格式化程序的所有尝试都没有产生任何结果。 自定义Annotation格式化程序应格式化LocalDate,LocalDateTime和Dates。
即使是预先存在的spring格式化程序(@DateTimeFormat,@ NumberFormat)也不起作用。
这篇文章Spring: custom validator is not being called类似,但我没有注意到我的代码中存在任何不匹配。
关于自定义格式化程序的在线示例使用jsp来获取值。它们可以用于基于弹簧休息的项目而不是mvc吗?可能我使用格式化程序的方式不正确。
非常感谢任何帮助。
由于
public class BroadcastDateFormatter implements Formatter<LocalDate>
{
@Override
public String print(LocalDate object, Locale locale)
{
// TODO Auto-generated method stub
return null;
}
@Override
public LocalDate parse(String text, Locale locale)
throws ParseException
{
// TODO Auto-generated method stub
return null;
}
}
public class BroadcastLocalDateFormatterAnnotationFactory implements AnnotationFormatterFactory<BroadcastLocalDateFormat>
{
@Override
public Set<Class<?>> getFieldTypes()
{
Set<Class<?>> classes = new HashSet<>();
classes.add(LocalDate.class);
classes.add(LocalDateTime.class);
classes.add(Date.class);
return classes;
}
@Override
public Printer<?> getPrinter(BroadcastLocalDateFormat annotation, Class<?> fieldType)
{
return new BroadcastDateFormatter();
}
@Override
public Parser<?> getParser(BroadcastLocalDateFormat annotation, Class<?> fieldType)
{
return new BroadcastDateFormatter();
}
}
@Configuration
@EnableWebMvc
@EnableJpaRepositories(basePackages = { "com.test.*" })
@EntityScan(basePackages = { "com.test.*" })
@EnableAutoConfiguration(exclude = { DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
public class AppConfig
extends WebMvcConfigurerAdapter
{
@Override
public void addFormatters(FormatterRegistry registry)
{
registry.addConverter(new FilterExpressionConverter());
registry.addFormatterForFieldAnnotation(new BroadcastLocalDateFormatterAnnotationFactory());
registry.addFormatterForFieldAnnotation(new SSNAnnotationFormatterFactory());
registry.addFormatter(new SSNFormatter());
registry.addFormatter(new BroadcastDateFormatter());
}
public class FooDomain
{
@JsonProperty(value="my_current_date")
@DateTimeFormat(pattern = "dd/MM/yyyy")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-d")
private LocalDate currentDate;
@BroadcastLocalDateFormat
private LocalDate broadcastDate;
@NumberFormat(style=Style.CURRENCY, pattern = "#,###.###")
private Double currency;
}
output:
{
"string": "foo",
"lng": 1,
"integer": 1,
"dbl": 1.2,
"flt": 1.3,
"bigDecimal": 1.1,
"innerClass": {
"string": "foo",
"lng": 1,
"integer": 1,
"dbl": 1.2,
"flt": 1.3,
"bigDecimal": 1.1
},
"innerClassesList": [
{
"string": "foo",
"lng": 1,
"integer": 1,
"dbl": 1.2,
"flt": 1.3,
"bigDecimal": 1.1
},
{
"string": "foo",
"lng": 1,
"integer": 1,
"dbl": 1.2,
"flt": 1.3,
"bigDecimal": 1.1
}
],
"innerClassSet": [
{
"string": "foo",
"lng": 1,
"integer": 1,
"dbl": 1.2,
"flt": 1.3,
"bigDecimal": 1.1
},
{
"string": "foo",
"lng": 1,
"integer": 1,
"dbl": 1.2,
"flt": 1.3,
"bigDecimal": 1.1
}
],
"innerClassMap": {
"1": {
"string": "foo",
"lng": 1,
"integer": 1,
"dbl": 1.2,
"flt": 1.3,
"bigDecimal": 1.1
}
},
"someEnum": "FOO",
"someObject": null,
"bigDecimalPct": 1.1,
"currency": 1001.3,
"javUtilDate": "2017-01-16 20:20 PM UTC",
"broadcastDate": [
2017,
1,
16
],
"broadcastDateTime": [
2017,
1,
16,
15,
20,
43,
7000000
],
"ssn": {
"a": "a",
"b": "b"
},
"my_current_date": "2017-01-16"
}