springmvc返回不帶引號的字串
springmvc返回不帶引號的字串
專案使用springboot開發的,大部分出參為json,使用的fastJson,現在有的介面需要返回一個success字串,發現返回結果為“success”,因為fastJson對出參做了處理,現在新增string型別的解析器就可以了,程式碼如下:
@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setFeatures(SerializerFeature.DisableCircularReferenceDetect);
converter.setCharset(Charset.forName("UTF-8"));
return converter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(stringHttpMessageConverter());
converters.add(fastJsonHttpMessageConverter());
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.clear();
converters.add(stringHttpMessageConverter());
converters.add(fastJsonHttpMessageConverter());
}
}
**
注意:string型別的解析器要放到json的前