在spring批处理应用程序中添加转换服务的不同方法

bwntbbo3  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(212)

我最近在和spring合作。我们需要将localdate和localdatetime转换为特定的格式。
为此,我编写了两种不同的转换器方法来实现这一点。但是这两种方法我已经注册了fieldsetmapper。但我无法转换日期。
如何在linemapper方法中添加两个不同的转换服务?为什么会出现类型不匹配错误?
我已经尝试了可能的方法来确定它背后的问题,但无法得到它。请指导我如何解决这个问题?
即使添加了转换器,我也会面临类型不匹配的错误?我如何解决这个问题?

  1. primary question
  2. 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?
  3. 2) After adding converters still why i am getting Type mismatch Error? why it is showing can not convert String to required type?

实用程序类

这是一个实用程序类,负责转换stringtolocaldateconversion。

  1. Class -1
  2. public final class StringToLocalDateConversion {
  3. private StringToLocalDateConversion() {
  4. }
  5. public static ConversionService createLocalDateConversionServicve() {
  6. DefaultConversionService stringToLocalDateconversionService = new DefaultConversionService();
  7. DefaultConversionService.addDefaultConverters(stringToLocalDateconversionService);
  8. stringToLocalDateconversionService.addConverter(new Converter<String, LocalDate>() {
  9. @Override
  10. public LocalDate convert(String text) {
  11. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-DD");
  12. return LocalDate.parse(text, formatter);
  13. }
  14. });
  15. return stringToLocalDateconversionService;
  16. }
  17. }

如果有任何错误,请建议我,我会改进?
这是一个实用程序类,负责转换stringtolocaldateconversion。

  1. Class 2
  2. public final class StringToLocalDateTimeConversion {
  3. public StringToLocalDateTimeConversion() {
  4. }
  5. public static ConversionService stringToLocalDateTimeConversionService() {
  6. DefaultConversionService stringToLocalDateTimeConversion = new DefaultConversionService();
  7. DefaultConversionService.addDefaultConverters(stringToLocalDateTimeConversion);
  8. stringToLocalDateTimeConversion.addConverter(new Converter<String, LocalDateTime>() {
  9. @Override
  10. public LocalDateTime convert(String source) {
  11. DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  12. return LocalDateTime.parse(source, dateTimeFormatter);
  13. }
  14. });
  15. return stringToLocalDateTimeConversion;
  16. }
  17. }

如果有任何错误,请建议我,我会改进?

batchconfiguration类

我在linemapper方法中添加了这两个转换器。

  1. @Bean
  2. public LineMapper<Customer> linemapper() {
  3. DefaultLineMapper<Customer> linemapper = new DefaultLineMapper<>();
  4. final DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
  5. tokenizer.setDelimiter(";");
  6. tokenizer.setStrict(false);
  7. tokenizer.setNames(new String[] { "id_type", "id_number", "customer_name", "email_address", "birthday",
  8. "citizenship", "address", "msisdn", "kyc_date", "kyc_level", "goalscore", "mobile_network" });
  9. BeanWrapperFieldSetMapper<Customer> fieldSetMapper = new BeanWrapperFieldSetMapper<>();
  10. fieldSetMapper.setTargetType(Customer.class);
  11. ConversionService localDateConversionService =
  12. StringToLocalDateConversion.createLocalDateConversionServicve();
  13. ConversionService localDateTimeConversionService = StringToLocalDateTimeConversion
  14. .stringToLocalDateTimeConversionService();
  15. fieldSetMapper.setConversionService(localDateConversionService);
  16. fieldSetMapper.setConversionService(localDateTimeConversionService);
  17. linemapper.setFieldSetMapper(fieldSetMapper);
  18. linemapper.setLineTokenizer(tokenizer);
  19. System.out.println(tokenizer);
  20. System.out.println(fieldSetMapper);
  21. return linemapper;
  22. }

堆栈跟踪错误

  1. Caused by: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 3 errors
  2. 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"]
  3. 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]
  4. 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]

请团队在这方面指导我?紧急要求。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题