Spring GenericConversionService provides method to convert one data type to another data type, here is one to convert String to Integer and String to List.
Example
package com.pretech;import java.io.IOException;import java.util.List;import org.springframework.core.convert.support.ConversionServiceFactory;import org.springframework.core.convert.support.GenericConversionService;public class SpringTypeConverterExample {public static void main(String[] args) throws IOException {GenericConversionService springconversionService = ConversionServiceFactory.createDefaultConversionService();//Integer to StringString str = springconversionService.convert(222,String.class);System.out.println(str);//String to IntegerInteger integer = springconversionService.convert("100",Integer.class);System.out.println(integer);//Comma separated String to ListList<String> list = springconversionService.convert("sunday,monday,tuesday",List.class);for(String st: list){System.out.println("List values :"+st);}}}
Output
222
100
List values :sunday
List values :monday
List values :tuesday
No comments:
Post a Comment