框架版本
springboot 2.2.1.RELEASE
fastjson 1.2.78
maven pom.xml parent 和 dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
</dependencies>
问题
如下代码,返回结果是 "hello world",添加了一个双引号。
@RestController
@Slf4j
public class Hello1Controller {
@GetMapping(value = "/printHello")
@ResponseBody
public String printHello() {
return "hello world";
}
}
排查
不管返回什么的结果,肯定经过MessageConvert进行消息的输出,debug看使用了哪一个MessageConvert
选择MessageConvert的方法是
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor#writeWithMessageConverters(T, org.springframework.core.MethodParameter, org.springframework.http.server.ServletServerHttpRequest, org.springframework.http.server.ServletServerHttpResponse)
关键代码
if (selectedMediaType != null) {
selectedMediaType = selectedMediaType.removeQualityValue();
// 打个断点在此处
for (HttpMessageConverter<?> converter : this.messageConverters) {
GenericHttpMessageConverter genericConverter = (converter instanceof GenericHttpMessageConverter ?
(GenericHttpMessageConverter<?>) converter : null);
if (genericConverter != null ?
((GenericHttpMessageConverter) converter).canWrite(targetType, valueType, selectedMediaType) :
converter.canWrite(valueType, selectedMediaType)) {
body = getAdvice().beforeBodyWrite(body, returnType, selectedMediaType,
(Class<? extends HttpMessageConverter<?>>) converter.getClass(),
inputMessage, outputMessage);
if (body != null) {
Object theBody = body;
LogFormatUtils.traceDebug(logger, traceOn ->
"Writing [" + LogFormatUtils.formatValue(theBody, !traceOn) + "]");
addContentDispositionHeader(inputMessage, outputMessage);
if (genericConverter != null) {
genericConverter.write(body, targetType, selectedMediaType, outputMessage);
}
else {
((HttpMessageConverter) converter).write(body, selectedMediaType, outputMessage);
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Nothing to write: null body");
}
}
return;
}
}
}
debug发现我的messageConvert只有一个,是com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter。那肯定是经过FastJson将String 写到输出流。
debug查看FastJsonHttpMessageConverter具体的write 代码 ,可以看到此时输出字符串添加了引号。
方法:com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter#writeInternal
写个fastjson 转化字符串试试,如下结果输出是 "hello world",添加了引号。
import com.alibaba.fastjson.JSON;
public class JsonTest {
public static void main(String[] args) {
System.out.println(JSON.toJSONString("hello world"));
//
}
}
问题所在
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setCharset(StandardCharsets.UTF_8);
//设置允许返回为null的属性
config.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
fastJsonConverter.setFastJsonConfig(config);
List<MediaType> list = new ArrayList<>();
list.add(MediaType.APPLICATION_JSON);
fastJsonConverter.setSupportedMediaTypes(list);
converters.add(fastJsonConverter);
}
}
我配置了FastJsonHttpMessageConverter ,导致只使用FastJsonHttpMessageConverter 进行消息转换。
解决方法
添加StringHttpMessageConverter ,让其转化String。注意顺序,StringHttpMessageConverter 要在FastJsonHttpMessageConverter 之前。
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setCharset(StandardCharsets.UTF_8);
//设置允许返回为null的属性
config.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
fastJsonConverter.setFastJsonConfig(config);
List<MediaType> list = new ArrayList<>();
list.add(MediaType.APPLICATION_JSON);
fastJsonConverter.setSupportedMediaTypes(list);
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setSupportedMediaTypes(list);
converters.add(stringHttpMessageConverter);
converters.add(fastJsonConverter);
}
返回数据没有引号了。
参考资料
【Spring】HttpMessageConverter的作用及替换
到此这篇关于springboot 接口返回字符串带引号的问题解决的文章就介绍到这了,更多相关springboot 返回字符串带引号内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!