SpringMVC_自定义类型转换器(springboot)

@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
    @Override
    public void addFormatters(FormatterRegistry registry){
        Converter<String,Person> personConverter = new Converter<String,Person>(){
            @Override
            public Person convert(String content){
                Person person = new Person();
                String[] split = content.split(",");
                person.setName(split[0]);
                person.setAge(Integer.parseInt(split[1]));
                person.setSex(split[2]);
                return person;
            }
        };
        registry.addConverter(personConverter);
    }
}

  

posted @ 2022-09-23 10:42  疾风儿  阅读(60)  评论(0编辑  收藏  举报