springBoot之基础篇

关于如何去创建springBoot项目,本人就不多说了.用idea创建springBoot是非常快捷的,详情请参照一下连接,

用idea快速搭建springBoot项目

1.废话不多说直接来一个入门helloword

@RestController作用:
  注解整合了@Controller和@ResponseBody。使用了这个注解的类会被看作一个controller。controller中使用@RequestMapping的方法有一个默认的@ResponseBody注解。@ResponseBody也可以加到类一级,通过继承方法一级不需要添加。
@Responsebody 作用:
  该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:
  返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/sayHello")
    public String sayHello(){
        return  "hello springBoot";
    }

}

2.springBoot配置文件

配置文件(两种写法)主要application.properties 写法如下 

home.province=浙江
home.city=WenZhou
home.desc=dev:I'm living in ${home.province} ${home.city}.

还可以写在    application.ym文件中,

user:
      id:   ${random.long}
      age: ${random.int[0,200]}
      desc: 我是MT ${random.value}
      uuid: ${random.uuid}

配置好就可以写实体类了如下代码:@Compoent表示将该实体类注入到springBoot中,ID注入

ConfigurationProperties根据类型校验和管理application中的bean

package com.springboot.springbootdemo.property;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "user")
public class UserProperties {
    private Long id;

    private int age;

    private String desc;

    private String uuid;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    @Override
    public String toString() {
        return "UserProperties{" +
                "id=" + id +
                ", age=" + age +
                ", desc='" + desc + '\'' +
                ", uuid='" + uuid + '\'' +
                '}';
    }
}

测试类如下:

package com.springboot.springbootdemo;


import com.springboot.springbootdemo.property.UserProperties;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class PropertiesTest {
    private static final Logger logger = LoggerFactory.getLogger(PropertiesTest.class);

    @Autowired
    private UserProperties userProperties;

    @Test
    public void userRandom(){
        logger.info("\n\n" + userProperties.toString() + "\n");

        System.out.println("************userProperties.toString()********"+userProperties.toString());
    }
}

猜你喜欢

转载自blog.csdn.net/qq_22899021/article/details/80921747