今天在linux系统中部署的项目启动的时候,在浏览器中弹出下载文件,在网上参考了下别人的文章,在启动类中加入一个bean,通过text-plian来解决。
依赖的jar包:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
public class WebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
/**
* 解决ie数据保存出现下载对话框的问题
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
//1.需要定义一个convert转换消息的对象;
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//2处理ie浏览器保存数据时出现下载json数据问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.TEXT_PLAIN);
//3.在convert中添加配置信息.
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
加入这个Bean之后,终于实现了不用下载就可以查看返回结果,但是返回的中文出现了乱码,经过一番查找源代码,发现通过如下方式来解决这个问题:即不使用MediaType.TEXT_PLAIN属性,而是通过new出新对象来解决。new MediaType("text", "plain", Charset.forName("UTF-8")。也可以使用下文中的方法,将这个返回的json传进行格式化处理,至此问题完美解决。
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
//1.需要定义一个convert转换消息的对象;
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//2处理ie浏览器保存数据时出现下载json数据问题
List<MediaType> fastMedisTypes = new ArrayList<>();
//fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);
//fastMedisTypes.add(MediaType.TEXT_PLAIN);
fastMedisTypes.add(new MediaType("text", "plain", Charset.forName("UTF-8")));
//3.在convert中添加配置信息.
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMedisTypes);
HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
return new HttpMessageConverters(converter);
}
Spring boot,通过创建实体类,然后通过实体类返回则就是json的格式,所以使用spring boot,不用担心返回格式处理不了的问题。springBoot,默认使用的json解析框架是Jackson。虽然jackson能够满足json的解析,但是心里最熟悉的依旧是alibaba的fastjon,当然,fastjon的要求springboot也是可以满足的,我们只需要在pom文件中配置maven依赖就好。
maven依赖完成之后,我们可以通过两种方式配置fastjon
方法一:启动类继承extends WebMvcConfigurerAdapter,且覆盖configureMessageConverters
package springboot;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
/* 1、先定义一个convert转换消息的对象 2、添加fastjson的配置信息,比如是否要格式化返回的json数据;
3、在convert中添加配置信息 4、将convert添加到converters*/
//1、先定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息,比如是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures( SerializerFeature.PrettyFormat); //是否需要格式化
//附加:处理中文乱码(后期添加)
List<MediaType> fastMedisTypes=new ArrayList<MediaType>();
fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMedisTypes);
//3、在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、将convert添加到converters
converters.add(fastConverter);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
如何判定使用的是fastJson呢?我们在实体类上,添加如上一个时间参数:
private int id;
private String name;
//添加一个当前时间字段
@JSONField(format = "yyyy-MM-dd HH:mm")
private Date createTime;
/* * 不想返回该属性, serialize:是否进行序列化 */ @JSONField(serialize = false) private String remarks;//备注信息
(2)若不想让接口返回一个属性,则设置 @JSONField 的 serialize 属性为false(不进行序列化);
@JSONField 就是基于fastjson的一种格式转换,通过返回值看到了我们想要的日期效果,所以这种json则是通过fastjson解析出来的;
如果解析数据遇到乱码问题的话,则通过上述代码段中添加的处理中文乱码方式来解决。
除此之外,我们还可以通过@JSONField(serialize = false)来决定字段的显示与否,也可以解决我们日常过多的字段不需要返回的问题。
方法二:在启动类中,注入Bean:HttpMessageConverters
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
//1、先定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息,比如是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig=new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//附加:处理中文乱码
List<MediaType> fastMedisTypes = new ArrayList<MediaType>();
fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMedisTypes);
//3、在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter=fastConverter;
return new HttpMessageConverters(converter);
}
其实代码的核心是相同的,这是调取的方式不同而已。两种方式都可以满足我们对于fastjson的依赖使用。