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>

發佈了130 篇原創文章 · 獲贊 127 · 訪問量 115萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章