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如何带参访问。

  以上内容若有出错,请不舍赐教。谢谢

相关文章
相关标签/搜索