版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29519041/article/details/85085380
之前环境eclipse+ssm 运行一切ok
之后环境idea+maven+springboot 出错
博主在出现这个问题的时候是在把ssm项目迁移成springboot中时出现的。
也就是说ssm项目中可以正常返回数据,springboot就不行了。
一般这种问题是controller返回的数据类型不符合ajax要求的数据类型导致!!
一般这种问题是controller返回的数据类型不符合ajax要求的数据类型导致!!
一般这种问题是controller返回的数据类型不符合ajax要求的数据类型导致!!
比如ajax需要的是文本类型,但是返回一个对象,那么ajax就会往error走。
不确定的可以在ajax中加入
error:function(){}
看看是否会执行function代码,当然里面写alert ()或console.log()都是可以的。
比如我在ssm框架下使用了fastjson,但是springboot中默认使用的是Jackson。
虽然我在springboot中导入了fastjson的依赖!
但是我发现在springboot中集成fastjson是需要做其他的步骤的
1.把json改为test。
2.或乖乖做完集成fastjson的其他操作
参考其他博客我运行了其中添加@bean注解的方式,亲测可行!
package com.anyunpei;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@MapperScan("com.anyunpei.dao")
public class Application {
@Bean
public HttpMessageConverters fastJsonConfigure(){
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//日期格式化
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
converter.setFastJsonConfig(fastJsonConfig);
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
在springboot中的启动类中添加@bean注解下的代码。注意加完这段代码后这里需要添加很多依赖,idea中使用alt+enter导入需要的依赖。
当然pom.xml中需要添加fastjson的依赖
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>