我最近在和spring合作。我们需要将localdate和localdatetime转换为特定的格式。
为此,我编写了两种不同的转换器方法来实现这一点。但是这两种方法我已经注册了fieldsetmapper。但我无法转换日期。
如何在linemapper方法中添加两个不同的转换服务?为什么会出现类型不匹配错误?
我已经尝试了可能的方法来确定它背后的问题,但无法得到它。请指导我如何解决这个问题?
即使添加了转换器,我也会面临类型不匹配的错误?我如何解决这个问题?
primary question
1) I have two converters for LocalDate & LocalDateTime.the way i have added the two converter class inside my linemapper() method is this correct approach or not? if not what approach i need to follow?
2) After adding converters still why i am getting Type mismatch Error? why it is showing can not convert String to required type?
实用程序类
这是一个实用程序类,负责转换stringtolocaldateconversion。
Class -1
public final class StringToLocalDateConversion {
private StringToLocalDateConversion() {
}
public static ConversionService createLocalDateConversionServicve() {
DefaultConversionService stringToLocalDateconversionService = new DefaultConversionService();
DefaultConversionService.addDefaultConverters(stringToLocalDateconversionService);
stringToLocalDateconversionService.addConverter(new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String text) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-DD");
return LocalDate.parse(text, formatter);
}
});
return stringToLocalDateconversionService;
}
}
如果有任何错误,请建议我,我会改进?
这是一个实用程序类,负责转换stringtolocaldateconversion。
Class 2
public final class StringToLocalDateTimeConversion {
public StringToLocalDateTimeConversion() {
}
public static ConversionService stringToLocalDateTimeConversionService() {
DefaultConversionService stringToLocalDateTimeConversion = new DefaultConversionService();
DefaultConversionService.addDefaultConverters(stringToLocalDateTimeConversion);
stringToLocalDateTimeConversion.addConverter(new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String source) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return LocalDateTime.parse(source, dateTimeFormatter);
}
});
return stringToLocalDateTimeConversion;
}
}
如果有任何错误,请建议我,我会改进?
batchconfiguration类
我在linemapper方法中添加了这两个转换器。
@Bean
public LineMapper<Customer> linemapper() {
DefaultLineMapper<Customer> linemapper = new DefaultLineMapper<>();
final DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setDelimiter(";");
tokenizer.setStrict(false);
tokenizer.setNames(new String[] { "id_type", "id_number", "customer_name", "email_address", "birthday",
"citizenship", "address", "msisdn", "kyc_date", "kyc_level", "goalscore", "mobile_network" });
BeanWrapperFieldSetMapper<Customer> fieldSetMapper = new BeanWrapperFieldSetMapper<>();
fieldSetMapper.setTargetType(Customer.class);
ConversionService localDateConversionService =
StringToLocalDateConversion.createLocalDateConversionServicve();
ConversionService localDateTimeConversionService = StringToLocalDateTimeConversion
.stringToLocalDateTimeConversionService();
fieldSetMapper.setConversionService(localDateConversionService);
fieldSetMapper.setConversionService(localDateTimeConversionService);
linemapper.setFieldSetMapper(fieldSetMapper);
linemapper.setLineTokenizer(tokenizer);
System.out.println(tokenizer);
System.out.println(fieldSetMapper);
return linemapper;
}
堆栈跟踪错误
Caused by: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 3 errors
Field error in object 'target' on field 'id_type': rejected value ["LISONDRA, MARIA MINI, JUCOM",MJLISONDRA71@GMAIL.COM,1971-02-12,FILIPINO,2,06-1401967-8,"M L QUEZON CABANCALAN, QUEZON, MANDAUE",9052100646,2019-10-10 11:45:18,FULL KYC,525_549,Globe Prepaid]; codes [typeMismatch.target.id_type,typeMismatch.id_type,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.id_type,id_type]; arguments []; default message [id_type]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'id_type'; nested exception is java.lang.NumberFormatException: For input string: ""LISONDRA,MARIAMINI,JUCOM",MJLISONDRA71@GMAIL.COM,1971-02-12,FILIPINO,2,06-1401967-8,"MLQUEZONCABANCALAN,QUEZON,MANDAUE",9052100646,2019-10-1011:45:18,FULLKYC,525_549,GlobePrepaid"]
Field error in object 'target' on field 'kyc_date': rejected value []; codes [typeMismatch.target.kyc_date,typeMismatch.kyc_date,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.kyc_date,kyc_date]; arguments []; default message [kyc_date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'kyc_date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDateTime] for value ''; nested exception is java.time.format.DateTimeParseException: Text '' could not be parsed at index 0]
Field error in object 'target' on field 'birthday': rejected value []; codes [typeMismatch.target.birthday,typeMismatch.birthday,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.birthday,birthday]; arguments []; default message [birthday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'birthday'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'birthday': no matching editors or conversion strategy found]
请团队在这方面指导我?紧急要求。
暂无答案!
目前还没有任何答案,快来回答吧!