springboot整合fastJson遇到重定向问题
通过网上教程使用springboot整合fastJson后遇到页面重定向问题(使用的springboot版本是2.0.2.RELEASE ,其他的版本可能不会出现以下问题),如下图:
我的项目结构如下
自定义的fastJson消息转换器配置类(WebMvcConfigurerAdapter这个类好像在springboot2.0以及spring5.0的版本后被废弃了所以通过继承WebMvcConfigurationSupport来实现configureMessageConverters方法的重写),代码如下:
package com.boot.interceptor; import java.util.ArrayList;
import java.util.List; import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @Configuration
public class FastJsonConfiguration extends WebMvcConfigurationSupport{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
// 创建FastJson消息转换器
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
// 创建配置类
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// 修改配置返回内容的过滤
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
); List<MediaType> fastMediaType = new ArrayList<>();
fastMediaType.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaType);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
// 将FastJson添加到视图消息转换器列表内
converters.add(fastJsonHttpMessageConverter);
} }
在mvc.properties中配置了spring.mvc.view.prefix和spring.mvc.view.suffix,配置如下
controller包中TestController测试类代码如下
package com.boot.interceptor.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class TestController { @RequestMapping("/test")
public String test() throws {
return "index";
} }
index.jsp页面
将FastJsonConfiguration这个类注释掉后就可以正常返回,效果如下:
解决方案:
添加配置类生成UrlBasedViewResolver配置的Bean(@ConfigurationProperties注解的location属性好像在springboot1.5后已经没有了,所以通过@PropertySource可引入自定义配置文件),代码如下:
package com.boot.interceptor; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver; @Configuration
@ConfigurationProperties(prefix="spring.mvc.view")
@PropertySource("classpath:mvc.properties")
public class DefaultConfiguration { private String prefix;
private String suffix; public String getPrefix() {
return prefix;
} public void setPrefix(String prefix) {
this.prefix = prefix;
} public String getSuffix() {
return suffix;
} public void setSuffix(String suffix) {
this.suffix = suffix;
} @Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix(prefix);
resolver.setSuffix(suffix);
resolver.setCache(true);
resolver.setViewClass(JstlView.class);
return resolver;
}
}
问题是解决了,但是具体的原理问题我还没弄清楚,也希望园内的大佬指点下!
springboot整合fastJson遇到重定向问题的更多相关文章
- SpringBoot整合FastJson(七)
一.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson&l ...
- springboot整合fastjson 将null转成空字符串
/** * @Auther: mxf * @Date: 2019/4/18 09:12 * @Description: */ @Configuration @EnableWebMvc public c ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- SpringBoot整合Kafka和Storm
前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...
- SpringBoot整合RabbitMQ-整合演示
本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...
- SpringBoot整合Netty并使用Protobuf进行数据传输(附工程)
前言 本篇文章主要介绍的是SpringBoot整合Netty以及使用Protobuf进行数据传输的相关内容.Protobuf会简单的介绍下用法,至于Netty在之前的文章中已经简单的介绍过了,这里就不 ...
- SpringBoot整合Jsp和Thymeleaf (附工程)
前言 本篇文章主要讲述SpringBoot整合Jsp以及SpringBoot整合Thymeleaf,实现一个简单的用户增删改查示例工程.事先说明,有三个项目,两个是单独整合的,一个是将它们整合在一起的 ...
- SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置
接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...
随机推荐
- vue的组件详解
什么是组件 组件(Component)是 Vue.js 最强大的功能之一.(好比电脑中的每一个元件(键盘,鼠标,CPU),它是一个具有独立的逻辑和功能或界面,同时又能根据规定的接口规则进行互相融合,变 ...
- python六十二课——高阶函数之filter
高阶函数之:filter函数:过滤数据的,最终返回一个惰性序列对象(filter对象,迭代器对象)解释:filter的意思:在计算机领域中我们都称为过滤器格式:filter(fn,lsd):参数和ma ...
- MongoDB 4.6.1 c++ driver 编译
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sheismylife/article/details/25512251 这个版本号已经和之前不一样了 ...
- oracle 查询分区表的最大分区以及分区字段的最大值
select 'select max(' || column_name || ') from ' || owner || '.' || name || ';' from DBA_PART_KEY_C ...
- ubuntu16.04安装配置nagios
参考博文:https://www.howtoing.com/ubuntu-nagios/ 该博文真实有效可供参考,按照步骤,基本可以成功 一.安装的先决条件 sudo apt-get install ...
- Dubbo -- 系统学习 笔记 -- 快速启动
Dubbo -- 系统学习 笔记 -- 目录 快速启动 服务提供者 服务消费者 快速启动 Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubb ...
- PAT A1126 Eulerian Path (25 分)——连通图,入度
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- 【ZOJ 3200】Police and Thief
ZOJ 3200 首先我写了个高斯消元,但是消出来了一些奇怪的东西,我就放弃了... 然后只好考虑dp:\(dp[i][j][k]\)表示走到了第i步,到了\((j,k)\)这个节点的概率. 那么答案 ...
- lesson 4:凯撒密码问题
一·问题描述 二·解决过程 1.设计思路 ①人机交互界面 ②加密函数(将字符串的每一个字符提取出来,通过对字符的阿斯克码值进行运算操作实现加密过程,特殊的特殊对待) ③解密函数(与加密函数算法类似) ...
- 51NOD1522 上下序列/CF567F Mausoleum DP
题目传送门:http://codeforces.com/problemset/problem/567/F 大致题意:你有$1$到$N$的所有正整数每个数两个,现在需要你将它排成一个序列,使得序列为单峰 ...