springboot整合fastJson遇到重定向問題

經過網上教程使用springboot整合fastJson後遇到頁面重定向問題(使用的springboot版本是2.0.2.RELEASE ,其餘的版本可能不會出現如下問題),以下圖:java

個人項目結構以下web

 

自定義的fastJson消息轉換器配置類(WebMvcConfigurerAdapter這個類好像在springboot2.0以及spring5.0的版本後被廢棄了因此經過繼承WebMvcConfigurationSupport來實現configureMessageConverters方法的重寫),代碼以下:spring

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,配置以下json

 

 controller包中TestController測試類代碼以下springboot

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頁面mvc

 

 

將FastJsonConfiguration這個類註釋掉後就能夠正常返回,效果以下:app

解決方案:jsp

添加配置類生成UrlBasedViewResolver配置的Bean(@ConfigurationProperties註解的location屬性好像在springboot1.5後已經沒有了,因此經過@PropertySource可引入自定義配置文件),代碼以下:ide

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;
	}
}

 

問題是解決了,可是具體的原理問題我還沒弄清楚,也但願園內的大佬指點下!測試

相關文章
相關標籤/搜索