使用ssm框架搭建工程完成easypoi导入excel文件时遇到的问题
1、IE8执行AJAX请求时,返回JSON出现下载文件(其实后台执行成功了,但是前台就是看着不爽)
解决方案:
- Produces返回类型改成"text/plain",(如果用的response那么就在消息头增加contentype为"text/plain"就好了)
- 如果上面使用的是produces的话,那么方法返回类型必须是String类型的,所以需要把json数据转成字符串,我用的fastjson(转换方式可以参考这个博主的:Java中fastjson库中Map、JSON、String相互转换),所以就是下面这样的
2、使用easypoi导入excel文件的时候,如果不想每一个字段都对应excel中一列的话,那么这个字段不加@Excel注解就行了
3、使用junit4进行单元测试,因为我需要自动注入service和mapper,就报错了:
java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith’s ‘value’ attribute or make the default bootstrapper class available.
查阅了网上资料,大部分都是以下情况:
-
包依赖没有或者版本不对,统一成spring框架的版本即可,可以用下面这种方式统一包的版本
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency>
-
如果还报错,看看你的junit4测试类写的对不对,注解加的对不对:
4、往tomcat部署的时候
- 第一个是部署报错,查看tomcat的启动日志发现找不到applicationContext.xml
看了一下target中确实没有我的那些配置文件
解决办法,这里需要配置一下pom.xml文件,添加一下配置:
修改后启动成功
- 第二个错误,启动后输入地址,找不到弹不出我的工程主页,报404找不到页面
原因是这里需要配置一下service.xml文件
在host节点下添加:
<Context docBase="score.war" path="/" debug="0" reloadable="true"/>
5、Tomcat部署好了,项目启动成功,但是查询的时候没有数据,看了一下tomcat的日志发现springmvc整合的mybatis查询报错了:
Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.bykj.mapper.StudentScoreMapper.selectByExample] with root cause
没有找到对应的mapper,看了一下target中,确实没有mapper的xml文件,上网查了一下,原来是maven没有编译,所以这里需要改一下pom.xml文件
在resources节点下添加以下代码:
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
6、过了几天把这个项目整合了一下swagger2在线api文档,不知道怎么搞得,后台传过来的json字符串是正常的json对象,但是前台获取的时候总是多一条 / ,所以就转不成json对象,于是又把返回类型改成了JSONObject就好了,另外ie浏览器竟然把汉字给解析成乱码了,所以又在produces上面加了编码格式:produces = “text/plain;charset=utf-8”,这样就解决了两个问题
这就和前面必须返回string类型矛盾了,可是现在不用返回string类型也不报错了,不知道是不是我改了fastjson的版本问题,这里记录一下我的fastjson版本是1.2.30:
<!--阿里json包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
7、springmvc整合fastjson之后,前台的swagger2生成的json文件是乱码的:
查了好半天,原来fastjson整合springmvc要跟spring的版本有关系,我就是用的4.2.1的spring结果用的是4.2以下的fastjson配置方案,这样就不对照了,参考这个博文解决了:(Fastjson、Jackson与SpringMVC整合的MessageConverter配置)https://my.oschina.net/liuyuantao/blog/796675
贴上解决方案:
1.Jackson
maven依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.7.1</version>
</dependency>
springmvc-servlet.xml中配置
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
2.FastJson
由于FastJson针对Spring4.2以后进行特殊优化,具体如图
所以FastJson可以分为Spring4.2及以下配置和Spring4.2以上的不同配置
Spring4.2及以下配置
maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
springmvc-servlet.xml中配置
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
<value>application/xml;charset=UTF-8</value>
</list>
</property>
<property name="features">
<list>
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Spring4.2以上配置
maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
springmvc-servlet.xml中配置
<mvc:annotation-driven validator="validator">
<mvc:message-converters register-defaults="true">
<ref bean="stringHttpMessageConverter"/>
<ref bean="fastJsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<!--FastJson(spring4.2x版本以上)-->
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" index="0"></constructor-arg>
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="fastJsonConfig">
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="features">
<list>
<value>AllowArbitraryCommas</value>
<value>AllowUnQuotedFieldNames</value>
<value>DisableCircularReferenceDetect</value>
</list>
</property>
<property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"></property>
</bean>
</property>
</bean>