SpringBoot入坑-配置文件使用

  經過上一篇的介紹,相信小夥伴們已經按奈不住內心對springboot的嚮往,本篇我將繼續向小夥伴介紹springboot配置文件的配置,已經全局配置參數如何使用,好了下面開始我們今天的內容介紹。

  我們知道Spring Boot支持容器的自動配置,默認是Tomcat,當然我們也是可以進行修改的:

  1、首先我們排除spring-boot-starter-web依賴中的Tomcat:在pom文件中排除tomcat的starter

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
      <exclusion>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
      </exclusion>
   </exclusions>
</dependency>

  2、加入Jetty容器

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

  這樣我們的springboot容器就修改成Jetty容器了。

  爲了方便我們的調試,這裏給大家推薦一款http調試工具:Postman

  下面我們聊一下springboot的全局配置文件:application.properties

  在開發中一定遇到過這樣的需求,就是修改我們的容器訪問端口,既然springboot默認加載容器,那麼端口設置當然是通過配置文件來控制的,相當方便我們只需要在配置文件中添加:

server.port=6666

  這樣我們的容器端口就修改爲6666了。

  我們還可以通過配置文件來設置項目訪問別名:

server.context-path=/springboot1

   這樣我們啓動項目通過http://localhost:6666/springboot1即可訪問到我們的項目

  以上只是springboot配置文件配置的冰山一角,比如我們還可以設置數據庫連接配置(database),設置開發環境配置,部署環境配置,實現兩者之間的無縫切換。
  下面我們一起了解一下關於springboot的controller的使用,springboot爲我們提供了三個註解:

  上一篇我們使用的便是@RestController,下面我們來一起使用@Controller試試:

@Controller
//@ResponseBody
public class RequestTest {

    /**
     * 不對請求方式限制
     * @return
     */
    @RequestMapping(value = "/req")
    public String req(){
        return "success";
    }
}

  當我們在瀏覽器輸入http://localhost:8080/springboot1/req回車,發現404

{
    "timestamp": 1515332935215,
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/springboot1/req"
}

  這是爲什麼呢?這是因爲@Controller必須配合模板使用,所以我們這裏打開maven的pom文件,添加spingboot的模板:

<!-- springboot模板 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

  然後在我們項目的resources目錄下找到templates(如過沒有,新建一個,但一定要注意文件夾名稱必須保持一致),然後創建一個success.html這樣我們再次啓動項目,訪問剛剛的地址,是不是就Ok了。

  不過這裏需要說明一點,現在的企業級開發都是前後端分離,我們做後臺服務只需要返回對應的數據即可,當然使用模板還有一個弊端,那就是性能會造成一定的損耗,所以這裏大家簡單瞭解即可。

  上面的介紹中已經說了,@Controller+@ResponseBody相當於@RestController,所以這裏推薦大家使用@RestController。

  下面我們來介紹介紹一下@RequestMapping(value = "/req"),這個註解相信大家已經知道他的用法了,當然這個註解不但可以使用在方法上,同樣適用於類。

@RestController
//@Controller
//@ResponseBody
@RequestMapping(value = "/test")
public class RequestTest {

    /**
     * 不對請求方式限制
     * @return
     */
    @RequestMapping(value = "/req")
    public String req(){
        return "success";
    }

    /**
     * 限制請求方式爲GET
     * @return
     */
    @RequestMapping(value = "/req1", method = RequestMethod.GET)
    public String req1(){
        return "success";
    }

    /**
     * 限制請求方式爲POST
     * @return
     */
    @RequestMapping(value = "/req2", method = RequestMethod.POST)
    public String req2(){
        return "success";
    }
}

  對於method相信看到這裏你一定已經知道他的用處了,是的指定訪問類型,沒有設置默認任何方式都可以訪問。不知道小夥伴是否想到如果在類的@RequestMapping設置過method那麼類中的方法默認繼承,當然也可以在方法處單獨設定,優先級的問題,小夥伴自己嘗試一下吧。

  下面我將給大家介紹一下如何在Controller中的訪問配置文件中的常量。首先我們在配置文件中添加:

name=hpugs
age=35
content=name:${name};age:${age}

  我們在配置文件中使用常量,通過${}來使用。

  下面我們在Controller中將參數注入:

    //注入配置文件中的參數
    @Value("${name}")
    private String name;
    @Value("${age}")
    private Integer age;
    @Value("${content}")
    private String content;

    @RequestMapping(value = "/req3", method = RequestMethod.GET)
    public String req3(){
        return "name=" + name;
    }

    @RequestMapping(value = "/req4", method = RequestMethod.GET)
    public String req4(){
        return "age=" + age;
    }

    @RequestMapping(value = "/req5", method = RequestMethod.GET)
    public String req5(){
        return "content=" + content;
    }

   啓動我們的項目訪問一下試試。

  這樣的使用如果你感覺還不過癮,這裏再教大家一招:我們通過類映射配置文件,藉助類來進行參數使用,相對單個參數注入要方便一些,首先創建一個Java類

@Component
@ConfigurationProperties(prefix = "userInfo")
public class UserInfo {

    private String names;
    private Integer age;
    private String content;

    public Integer getAge() {
        return age;
    }

    public String getNames() {
        return names;
    }

    public void setNames(String names) {
        this.names = names;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

  然後在我們的配置文件中設置參數:

userInfo.names=小破孩
userInfo.age=25
userInfo.content=name:${userInfo.names};age:${userInfo.age}

  接線來使我們的Controller:

    //注入對象
    @Autowired
    private UserInfo userInfo;

    @RequestMapping(value = "/req6", method = RequestMethod.GET, produces="text/plain;charset=UTF-8")
    public String req6(){
        return "name=" + userInfo.getNames();
    }

    @RequestMapping(value = "/req7", method = RequestMethod.GET)
    public String req7(){
        return "age=" + userInfo.getAge();
    }

    @RequestMapping(value = "/req8", method = RequestMethod.GET)
    public String req8(){
        return "content=" + userInfo.getContent();
    }

  好了重啓我們的項目訪問試試看。

  小夥伴們不知道遇到這個問題沒?出現了中文亂碼,首先大家先不要着急,我們先看另外一種springboot的配置文件:application.yml。這個配置文件通過換行空格來替換“;”,我們看一下同樣的配置在yml下面如何配置:

server:
  port: 8888
  context-path: /springboot1

name: hpugs
age: 35
content: name:${name};age:${age}

userInfo:
  names: 小破孩
  age: 25
  content: name:${userInfo.names};age:${userInfo.age}

  現在我們啓動項目運行試一試。

  回到上面的亂碼問題,當我們使用yml時是不是沒有出現亂碼,小夥伴是不是有點鬱悶了,這是爲什麼呢?這是因爲.properties文件使用的是unicode的編碼形式,所以當我們輸入中文時會出現亂碼。當然引亂碼的還有一種原因那就是我能的編碼設置和前端不一致,這個我們通過在配置文件中添加:

spring:
    http:
        encoding:
          force: true
          charset: UTF-8
        enabled: true
server:
  tomcat:
    uri-encoding: UTF-8

  來進行控制。這裏再給大家介紹一下開發小技巧,springboot爲我們提供了在不同開發環境下的不同配置文件解決方法:

#yml格式
spring:
    profiles:
      active: prod

#.properties格式
spring.profiles.active=dev

  好了到這裏關於springboot Controller的內容就先到這裏,下一篇springboot Controller如何帶參訪問。

  以上內容如有出錯,請不捨賜教。謝謝

 

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