zoukankan      html  css  js  c++  java
  • 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;
    	}
    }
    

    问题是解决了,但是具体的原理问题我还没弄清楚,也希望园内的大佬指点下!

  • 相关阅读:
    Params参数的应用
    用反射动态创建委托
    Javascript中的Array的运用
    四个JS代码与jquery架构简单运用
    base 基本用法转载自(MSDN)
    Microsoft AJAX Library 新特性简单代码
    典型的委托的不同写法(转载)
    创建反射实例|反射过滤|反射搜索
    【软工】第1次个人作业
    【软工】第0次个人作业
  • 原文地址:https://www.cnblogs.com/dyc940210/p/9178052.html
Copyright © 2011-2022 走看看