SpringBoot入门系列~SpringBoot属性配置1

前一章节主要是SpringBoot的简单介绍与项目的创建,这章节主要配置一些指定的属性,主要在application.properties 属性配置文件配置,具体实现看代码注释

  • 配置系统属性
#指定项目访问的端口,默认是8080端口
server.port=8088
#指定访问路径不是根目录,需要加项目名
server.context-path=/SpringBootChapter1

启动访问: http://127.0.0.1:8088/SpringBootChapter1/showMsg

  • 配置自定义属性:key=value的形式 
  • 在代码中通过注解获取
  • 定义方法访问
    /**
         * 测试从属性配置文件取值操作
        * @Title: customMessager 
        * @author sunt  
        * @date 2017年11月7日
        * @return String
         * @throws UnsupportedEncodingException 
         */
        @RequestMapping("/customMessager")
        @ResponseBody
        public String customMessager() throws UnsupportedEncodingException {
            return customMessager;
        }

    页面访问:http://127.0.0.1:8088/SpringBootChapter1/customMessager

  • 中文出现乱码解决方法代码

    /**
         * 测试从属性配置文件取值操作
        * @Title: customMessager 
        * @author sunt  
        * @date 2017年11月7日
        * @return String
         * @throws UnsupportedEncodingException 
         */
        @RequestMapping("/customMessager")
        @ResponseBody
        public String customMessager() throws UnsupportedEncodingException {
            //解决中文乱码问题
            return new String(customMessager.getBytes("iso-8859-1"), "utf-8");
        }

     

     

 svn地址:svn://gitee.com/SunnySVN/SpringBoot

posted @ 2017-11-07 14:34  sunny1009  阅读(165)  评论(0)    收藏  举报