Home > database >  Failed to convert property value of type java.lang.String[] to required type java.util.List spring m
Failed to convert property value of type java.lang.String[] to required type java.util.List spring m

Time:11-16

enter image description here

Failed to convert property value of type java.lang.String[] to required type java.util.List for property tags; nested exception is java.lang.IllegalStateException: Cannot convert value of type java.lang.String to required type edu.pxu.ri.MVCvaJPA.entity.Tag for property tags[0]: no matching editors or conversion strategy found

ManyToMany ORM

Solution: enter image description here

AppConfig: enter image description here

Happy to help you solve the problem.

CodePudding user response:

package:converter

 @Component
    public class TagConverter implements Converter<String[], List<Tag>> {
    
        @Autowired
        TagService tagService;
        
        @Override
        public List<Tag> convert(String[] arg) {
            // TODO Auto-generated method stub
             List<Tag> tagList = new ArrayList<>();
                System.out.println("tags String[]" arg);
                    for (String tag : (String[])arg) {
                        System.out.println("tagId:" tag);
                        Tag tagUd = tagService.getTagById(Integer.valueOf(tag));
                        tagList.add(tagUd);          
                    }
             return tagList;
        }
    
    }

CodePudding user response:

AppConfig

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
    "edu.pxu.ri.MVCvaJPA"
})
public class AppConfig implements WebMvcConfigurer {
    @Autowired
    TagConverter tagConverter;
    
    @Autowired
    TagConverterString tagConverterString;
    
    @Override
     public void addFormatters(FormatterRegistry registry) {
         registry.addConverter(tagConverter);
         registry.addConverter(tagConverterString);
    }
}
  • Related