SpringBoot入坑-配置文件使用

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

  咱們知道Spring Boot支持容器的自動配置,默認是Tomcat,固然咱們也是能夠進行修改的:前端

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

<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>

  二、加入Jetty容器spring

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

  這樣咱們的springboot容器就修改爲Jetty容器了。數據庫

  爲了方便咱們的調試,這裏給你們推薦一款http調試工具:Postman後端

  下面咱們聊一下springboot的全局配置文件:application.properties瀏覽器

  在開發中必定遇到過這樣的需求,就是修改咱們的容器訪問端口,既然springboot默認加載容器,那麼端口設置固然是經過配置文件來控制的,至關方便咱們只須要在配置文件中添加:tomcat

server.port=6666

  這樣咱們的容器端口就修改成6666了。springboot

  咱們還能夠經過配置文件來設置項目訪問別名:app

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如何帶參訪問。

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

相關文章
相關標籤/搜索