通過網上教程使用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;
}
}
問題是解決了,但是具體的原理問題我還沒弄清楚,也希望園內的大佬指點下!
