resteasy 數據校驗之Hibernate validation 國際化問題

resteasy 添加數據校驗參見官方文檔

步驟

  1. 添加jar包
javax.validation:validation-api:1.1.0.Final
org.jboss.resteasy:resteasy-validator-provider-11:3.0.18.Final
  1. 在接口參數用使用
public class CompanyNewsCommand {
	
	@NotBlank(message="{title.not.blank}")
	private String title;
	
	@NotBlank(message="{content.not.blank}")
	@Length(max=65535,message="{content.length.max}")
	private String content;
	.........
}
  1. 捕獲檢驗異常和message並輸出 提供bean,並且注入spring
@Provider
public class ValidationExceptionHandler implements ExceptionMapper<ResteasyViolationException> {

	private static final String JSON_CONTENT_TYPE = "application/json;charset=UTF-8";

	@Override
	public Response toResponse(ResteasyViolationException exception) {
		StatusType statusType = Status.BAD_REQUEST;
		String errorCode = Integer.toString(statusType.getStatusCode());
		StringBuilder sb = new StringBuilder();
		for (ResteasyConstraintViolation violation : exception.getViolations()) {
			sb.append(violation.getMessage());
			sb.append(" ");
		}
		RestResponse entity = new RestResponse(errorCode, sb.toString());
		ResponseBuilder builder = Response.status(statusType).entity(entity);
		builder.header("Content-Type", JSON_CONTENT_TYPE);
		return builder.build();
	}

}

具體的緩存和多文件讀取可以自行實現

4.國際化 在根目錄中放置ValidationMessages_zh_CN.properties文件

title.not.blank=\u6807\u9898\u4E0D\u80FD\u4E3A\u7A7A
content.length.max=\u5185\u5BB9\u8D85\u8FC7\u6700\u5927\u957F\u5EA6\u6216\u8005\u56FE\u7247\u8FC7\u5927

提示信息國際化

我們這裏主要講在集成過程中遇到的問題

  1. 國際化文本 ValidationMessages_zh_CN.properties 必須放在根目錄
  2. ValidationMessages_zh_CN.properties編碼類型必須是ISO-8859-1,不能是utf-8
  3. ValidationMessages_zh_CN.properties文件名稱不能更改
  4. 在非LANG=zh_CN.UTF-8環境中運行時國際化文本沒有起作用
服務器上
# echo $LANG
en_US.UTF-8

本地
zh_CN.UTF-8

問題原因

ResourceBundleMessageInterpolator的實現問題

  • 該MessageInterpolator 寫死了必須使用ValidationMessages_zh_CN.properties 文件,並且放置於根目錄
  • 該MessageInterpolator 使用java.util.ResourceBundle 來解析properties文件,ResourceBundle在讀取properties文件時統一使用iso8859-1編碼
  • ResourceBundleMessageInterpolator 使用PlatformResourceBundleLocator來加載ResourceBundle,根據操作系統的LANG設置來加載,要求平臺必須是LANG=zh_CN.UTF-8,並且該方式限制了只有一個properties 文件

對於國際化的問題,在springmvc+hivernate validation 中已經很好的解決了 但是對於resteasy,沒有看到很好的解決方案

解決方案

  1. 添加 /META-INF/validation.xml
<validation-config
	xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration">

	<message-interpolator>com.dzj.frw.rest.validation.MyResourceBundleMessageInterpolator</message-interpolator>

</validation-config>
  1. 重寫 ResourceBundleMessageInterpolator
public class MyResourceBundleMessageInterpolator extends org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator {
	private static final Logger LOGGER = LoggerFactory.getLogger(DzjResourceBundleMessageInterpolator.class);
	
	public DzjResourceBundleMessageInterpolator() {
	//在這裏改寫properties 文件名
		super(new PlatformResourceBundleLocator("ValidationMessages1"));
	}
	@Override
	public String interpolate(String message, Context context) {
	// 在這裏改寫編碼
		String result = super.interpolate(message, context);
		try {
			return new String(result.getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			return result;
		}
	}
}

進一步方案

這個方案還是有問題,不能支持多個文件的讀,我們完全可以自己實現javax.validation.MessageInterpolator,用java.util.properties 來讀取,並且支持多個文件,同時緩存內容到內存。

public class PropertiesMessageInterpolator  implements MessageInterpolator{

	@Override
	public String interpolate(String key, Context context) {
       String messageKey = StringUtils.trimPrefix(key, "{");
        messageKey = StringUtils.trimSuffix(messageKey, "}");
        Locale locale = LocaleContextHolder.getLocaleContext().getLocale();
        return interpolate(messageKey,context,locale);
	}

	@Override
	public String interpolate(String messageKey, Context context, Locale locale) {
        String message = MessageBundle.getInstance(locale).getString(messageKey);
		return message;
	}

}

具體的多文件讀取和緩存可以自己實現。

reference

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章