目录
5、通过HandlerExceptionResovler接口实现全局异常
6、使用@ControllerAdvice+@ExceptionHandler实现全局异常
一、json
1、导入pom依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.3</version>
</dependency>
2、springmvc-servlet.xml配置
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="mappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<!--处理中文乱码以及避免IE执行AJAX时,返回JSON出现下载文件-->
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
3、mapper.xml
<select id="MaplistPager" parameterType="com.cxy.model.Clazz" resultType="java.util.Map">
select
<include refid="Base_Column_List" />
from t_struts_class
<where>
<if test="cname != null and cname != ''">
cname like CONCAT('%',#{cname},'%')
</if>
<if test="cid != null and cid != ''">
cid = #{cid}
</if>
</where>
</select>
4、servlet层
public List<Map> mapListPager(Clazz clazz,PageBean pageBean) {
return clazzMapper.mapListPager(clazz);
}
5、后台代码
package com.cxy.controller;
import com.cxy.biz.ClazzBiz;
import com.cxy.model.Clazz;
import com.cxy.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//@ResponseBody
//@Controller
@RestController
@RequestMapping("/clz/json")
public class JsonController {
@Autowired
private ClazzBiz clazzBiz;
/**
* 返回JSON字符串
*/
@RequestMapping("/clzEdit")
public String clzEdit(){
return "JosnController.clzEdit";
}
/**
* 返回List<T>
*/
@RequestMapping("/list")
public List<Clazz> list(Clazz clazz, HttpServletRequest req){
PageBean pageBean = new PageBean();
pageBean.setRequest(req);