工作生活

04_Spring Boot 入门示例

2019-07-01  本文已影响0人  leofight

使用gradle构建项目

gradle 配置

buildscript {
    ext {
        springBootVersion = '2.1.3.RELEASE'
    }
    
    repositories {
        maven {
            url 'https://repo.spring.io/release'
        }
        
        mavenCentral()
    }
    
    dependencies {
        classpath (
            "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}",
            "io.spring.gradle:dependency-management-plugin:1.0.7.RELEASE"
        )
    }
}

apply{
    plugin("java")
    plugin("maven")
    plugin("idea")
    plugin("org.springframework.boot")
    plugin("io.spring.dependency-management")
}

group 'com.leofight'
version '1.0'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    maven {
        url 'https://repo.spring.io/release'
    }

    mavenCentral()
}

dependencies {
    compile (
        "org.springframework.boot:spring-boot-starter-web"
    )
}

新建MyApplication类

package com.leofight.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
    }
}


新建domain

package com.leofight.boot.domain;

import java.util.Date;

public class Person {

    private int id;

    private String name;

    private Date birthday;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}


新建controller

package com.leofight.boot.controller;

import com.leofight.boot.domain.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.http.MediaType;

import java.util.Date;


@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MyController {

    @RequestMapping(value = "/person",method = RequestMethod.GET)
    public Person getPerson() {
        Person person = new Person();

        person.setId(25);
        person.setName("zhangsan");
        person.setBirthday(new Date());

        return person;
    }
}

新建application.yml

server:
  port: 9090

Spring Boot提供了两种类型的配置文件形式
①properties文件形式

   server.port=9090
   server.test1=abc
   server.test2=xyz

②yml文件形式(YAML Yet Another Markup Language)

   server:
       port: 9090
       test1:abc
       test2:xyz

启动方式
①run Application
②gradle下点击bootRun
③terminal中执行gradle bootRun

浏览器中输入:http://localhost:9090/api/person

输出结果为:

{"id":25,"name":"zhangsan","birthday":"2019-06-30T06:46:39.213+0000"}

修改application.yml


spring:
 application:
   name: mytest
 mandatory-file-encoding: UTF-8
 http:
   encoding:
     enabled: true
     charset: UTF-8

server:
 port: 9090


myConfig:
 myObject:
   myName: zhangsan
   myAge: 20
   

读取自定义配置,新增如下两个类

package com.leofight.boot.config;

import org.springframework.beans.factory.annotation.Value;

public class MyConfigBean {

   @Value("${myConfig.myObject.myName}")
   private String myName;

   @Value("${myConfig.myObject.myAge}")
   private String myAge;


   public String getMyName() {
       return myName;
   }

   public void setMyName(String myName) {
       this.myName = myName;
   }

   public String getMyAge() {
       return myAge;
   }

   public void setMyAge(String myAge) {
       this.myAge = myAge;
   }
}




package com.leofight.boot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {

   @Bean
   public MyConfigBean myConfigBean(){
       return new MyConfigBean();
   }
}


使用

@Autowired
private MyConfigBean myConfigBean;
   
   System.out.println(myConfigBean.getMyAge()+"######"+myConfigBean.getMyName());



使用gradle bootJar命令打jar包,并解压jar查看目录结构

上一篇下一篇

猜你喜欢

热点阅读