Spring拾遗

使用@ControllerAdvice和@ExceptionHandler注解实现全局异常捕获

package com.mj.web.incpt;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.TypeMismatchException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import eric.test.Result;

@ControllerAdvice
public class GlobalExceptionHandler {
	
	@ExceptionHandler({ TypeMismatchException.class })
	@ResponseBody
	public Result<String> handleTypeMismatchException(HttpServletRequest request, Exception ex) {
		// TODO
		return null;
	}

}

@Controller和@RestController的区别

@RestController = @ResponseBody + @Controller
(1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return里的内容。
(2) 如果需要返回到指定页面,则需要用@Controller配合视图解析器InternalResourceViewResolver才行。
(3) 如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

@ResponseBody返回JSON设置不返回为null的值

在配置文件中加入如下配置,可使@ResponseBody不返回为null的值

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                    <property name="serializationInclusion">
                        <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
                    </property>
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

属性占位符PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。
在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改。
可借助PropertyPlaceholderConfigurer来满足不同环境的参数配置。

<!-- 本地服务器属性定制 -->
<bean id="propertyConfigure"
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="ignoreResourceNotFound" value="true" />
	<property name="ignoreUnresolvablePlaceholders" value="true" />
	<property name="fileEncoding" value="UTF-8" />
	<!-- 使用locations属性定义可多个配置文件 -->
	<property name="locations">
		<list>
			<value>file:/usr/local/eagle/conf/eagle.properties</value>
		</list>
	</property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
	destroy-method="close">
	<property name="driverClassName" value="${datasource.driver:com.mysql.jdbc.Driver}" />
	<property name="url"
		value="${datasource.url:jdbc:mysql://192.168.1.100:3306/test?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true}" />
	<property name="username" value="${datasource.username:root}" />
	<property name="password" value="${datasource.password:linkage@12345}" />
	<property name="validationQuery" value="SELECT 1" />
	<property name="initialSize" value="${datasource.initialSize:2}" />
	<property name="maxIdle" value="${datasource.maxIdle:2}" />
	<property name="maxActive" value="${datasource.maxActive:5}" />
	<property name="testOnBorrow" value="false" />
	<!-- (服务器端timeout=8小时、空闲超过8小时后、自动关闭客户端) -->
	<property name="testWhileIdle" value="true" />
	<property name="removeAbandoned" value="true" /> <!-- 是否自我中断, 默认是 true -->
	<property name="timeBetweenEvictionRunsMillis"
		value="${frontap.datasource.timeBetweenEvictionRunsMillis:30000}" /><!-- 
		每3-秒检查一次 -->
</bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值