与微信小程序进行交互式,提示出错因为前端传进来的是String,后端接收的是Integer,所以报错
写类型转换器:
@Component public class ConverterAction implements Converter<String,Integer> { @Override public Integer convert(String source) { if(StringUtils.isEmpty(source)){ return null; } Integer in=Integer.getInteger(source); return in; } @Override public JavaType getInputType(TypeFactory typeFactory) { return null; } @Override public JavaType getOutputType(TypeFactory typeFactory) { return null; } }
定义全局类型转换器的配置类
@Configuration @ComponentScan("com.example.demo.converter") public class ConerterConfig extends WebMvcConfigurerAdapter { @Bean @Autowired public ConversionService getConversionService(ConverterAction converterAction){ ConversionServiceFactoryBean factoryBean = new ConversionServiceFactoryBean(); Set<Converter> converters = new HashSet<Converter>(); converters.add((Converter) converterAction); factoryBean.setConverters(converters); return factoryBean.getObject(); } }
注意引用正确的转换器类:
import com.fasterxml.jackson.databind.util.Converter;