1、建立properts文件(error.message.properties)
HTTP201= 请求成功并且服务器创建了新的资源
2、在spring-mvc.xml文件(applicationContext-mvc.xml)中配置properties工具类路径及读取properties文件的路径
<bean id="propertyConfigurer" class="com.yjlc.platform.utils.PropertyConfigurer" >
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath:error.message.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"></property>
</bean>
3、建立properts读取工具类
package com.yjlc.platform.utils; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import java.util.Properties; /**
* --------------------------------------------------------------
* CopyRights(c)2018,YJLC
* All Rights Reserved
* <p>
* FileName: PropertyConfigurer.java
* Description:错误信息文件读取工具类
* Author: cyb
* CreateDate: 2019-01-18
* --------------------------------------------------------------
*/
public class PropertyConfigurer extends PropertyPlaceholderConfigurer {
private Properties props; // 存取properties配置文件key-value结果 @Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
super.processProperties(beanFactoryToProcess, props);
this.props = props; }
//输入配置文件中的key 获取对应的值
public String getProperty(String key){
return new String(props.getProperty(key)); } public String getProperty(String key, String defaultValue) {
return this.props.getProperty(key, defaultValue);
} public Object setProperty(String key, String value) {
return this.props.setProperty(key, value);
}
}
4、建立对应的service及实现类
(1) Service接口(GetMessageInfoService )
package com.yjlc.platform.upgrade.common.service; import java.io.UnsupportedEncodingException; /**
* --------------------------------------------------------------
* CopyRights(c)2018,YJLC
* All Rights Reserved
* <p>
* FileName: GetMessageInfoService.java
* Description:获取错误信息service
* Author: cyb
* CreateDate: 2019-01-18
* --------------------------------------------------------------
*/
public interface GetMessageInfoService { /**
* 第四种实现方式获取properties文件中指定key的value
*
* @param key
*
* @return
*/
String getProperyByFourthWay(String key) throws UnsupportedEncodingException; /**
* 第四种实现方式获取properties文件中指定key的value
*
* @param key
*
* @param defaultValue
*
* @return
*/
String getProperyByFourthWay(String key, String defaultValue); }
Service实现类
package com.yjlc.platform.upgrade.common.service.impl; import com.yjlc.platform.upgrade.common.service.GetMessageInfoService;
import com.yjlc.platform.utils.PropertyConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* --------------------------------------------------------------
* CopyRights(c)2018,YJLC
* All Rights Reserved
* <p>
* FileName: PropertiesServiceImpl.java
* Description:获取错误信息service实现类
* Author: cyb
* CreateDate: 2019-01-18
* --------------------------------------------------------------
*/
@Service
public class GetMessageInfoServiceImpl implements GetMessageInfoService { @Autowired
private PropertyConfigurer pc; @Override
public String getProperyByFourthWay(String key) {
return pc.getProperty(key);
} @Override
public String getProperyByFourthWay(String key, String defaultValue) {
return pc.getProperty(key, defaultValue);
} }
5、调用测试
@Controller
@RequestMapping("/upInformation/")
public class UPInformationController {
@Autowired
GetMessageInfoService getMessageInfoService; @RequestMapping("forwardInfo")
@ResponseBody
public Map<String,Object> forwardInfo(UPInformationComment informationComment,String status) throws UnsupportedEncodingException {
Map<String,Object> map = new HashMap<>(); String value= getMessageInfoService.getProperyByFourthWay("HTTP201"); } }
6、控制台打印中文乱码问题解决
找到intellij idea安装目录,bin文件夹下面idea64.exe.vmoptions和idea.exe.vmoptions这两个文件,分别在这两个文件中添加:-Dfile.encoding=UTF-8
第二步:找到intellij idea的file---settings---Editor---FileEncodings的GlobalEncoding和ProjectEncoding和Default encoding for properties都配置成UTF-8
第三步:在部署Tomcat的VM options项中添加:-Dfile.encoding=UTF-8
第四步:重启Intellij idea即可解决乱码问题(一定要关闭idea重新打开)
此篇博客,本人参照了以下博主的内容,再根据自己的需求进行了整合,若需要查看详细内容,大家可以前往以下链接查看:https://www.cnblogs.com/hafiz/p/5876243.html#commentform
技术在于交流!