通过网上教程使用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遇到重定向问题的更多相关文章

  1. SpringBoot整合FastJson(七)

    一.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson&l ...

  2. springboot整合fastjson 将null转成空字符串

    /** * @Auther: mxf * @Date: 2019/4/18 09:12 * @Description: */ @Configuration @EnableWebMvc public c ...

  3. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  4. springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...

  5. SpringBoot整合Kafka和Storm

    前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...

  6. SpringBoot整合RabbitMQ-整合演示

    本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...

  7. SpringBoot整合Netty并使用Protobuf进行数据传输(附工程)

    前言 本篇文章主要介绍的是SpringBoot整合Netty以及使用Protobuf进行数据传输的相关内容.Protobuf会简单的介绍下用法,至于Netty在之前的文章中已经简单的介绍过了,这里就不 ...

  8. SpringBoot整合Jsp和Thymeleaf (附工程)

    前言 本篇文章主要讲述SpringBoot整合Jsp以及SpringBoot整合Thymeleaf,实现一个简单的用户增删改查示例工程.事先说明,有三个项目,两个是单独整合的,一个是将它们整合在一起的 ...

  9. SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置

    接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...

随机推荐

  1. markdownpad破解

    下载在腾讯软件下载比较快. 破解我就不写步骤了,直接给个链接 https://blog.csdn.net/geekqian/article/details/74455409 授权邮箱:Soar360@ ...

  2. GUI概述与Frame演示

    java 图形化界面的对象存在这两个包中: java.awt :Abstract WindowsToolkit(抽象窗口工具包)需要调用本地系统方法实现功能,属重量级控件 javax.swing:在a ...

  3. Swift中的反射

    原文:http://www.cocoachina.com/applenews/devnews/2014/0623/8923.html Swift 事实上是支持反射的.只是功能略弱. 本文介绍主要的反射 ...

  4. yii2 对象跟数组输出数据到view视图方法

    public function actionJiekou(){ $url = 'http://wap.guoshihui.com/_static/wap/video/startVideo.mp4'; ...

  5. usb-cam (3)摄像机标定文件-ORB-SLAM标定文件

    http://www.luohanjie.com/2017-04-05/the-problem-of-calibration-data-in-orb-slam2.html ORB_SLAM2中标定数据 ...

  6. docker swarm 实例

    1.配置三台虚拟机 先在virtualbox上生成三个Linux主机,一个manager1(管理节点),两个工作节点worker1和worker2 1)manager1 userdeMacBook-P ...

  7. Python threading中lock的使用

    版权声明: https://blog.csdn.net/u012067766/article/details/79733801在多线程中使用lock可以让多个线程在共享资源的时候不会“乱”,例如,创建 ...

  8. Android学习之基础知识十六 — Android开发高级技巧的掌握

    一.全局获取Context的技巧 前面我们很多地方都使用到了Context,弹出Toast的时候.启动活动的时候.发送广播的时候.操作数据库的时候.使用通知的时候等等.或许目前来说我们并没有为得不到C ...

  9. Android学习之基础知识十二 — 第二讲:网络编程的最佳实践

    上一讲已经掌握了HttpURLConnection和OkHttp的用法,知道如何发起HTTP请求,以及解析服务器返回的数据,但是也许你还没发现,之前我们的写法其实是很有问题的,因为一个应用程序很可能会 ...

  10. Android学习之基础知识二(build.gradle文件详解)

    一.详解build.gradle文件 1.Android Studio是采用Gradle来创建项目的,Gradle是非常先进的构建的项目的工具,基于Groovy领域特定的语言(DSL)来声明项目配置, ...