spring boot 配置和启动

基于 maven框架下的spring boot 配置

1.pom.xml 声明资源

 
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.0.BUILD-SNAPSHOTversion>
    parent>

    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

    dependencies>

    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

    
    
    <repositories>
        <repository>
            <id>spring-snapshotsid>
            <url>http://repo.spring.io/snapshoturl>
            <snapshots><enabled>trueenabled>snapshots>
        repository>
        <repository>
            <id>spring-milestonesid>
            <url>http://repo.spring.io/milestoneurl>
        repository>
    repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshotsid>
            <url>http://repo.spring.io/snapshoturl>
        pluginRepository>
        <pluginRepository>
            <id>spring-milestonesid>
            <url>http://repo.spring.io/milestoneurl>
        pluginRepository>
    pluginRepositories>

2.编写一个controller类 (类似spring mvc)

import net.sf.json.JSONObject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
@EnableCaching
public class SpringBootTest {

    @RequestMapping("/test")
    public JSONObject test() {
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("name", "hello world!");

        return jsonObject;
    }

    public static void main(String[] args) {

        SpringApplication.run(SpringBootTest.class, args);
    }
}

其中 JSONObject 需要添加pom.xml 声明

  
        
        <dependency>
            <groupId>net.sf.json-libgroupId>
            <artifactId>json-libartifactId>
            <version>2.4version>
            <classifier>jdk15classifier>
        dependency>

JSON-lib提供了两个版本的SDK,如图
spring boot 配置和启动_第1张图片

所以jdk15要加 否则代码import不了,找不到对应jar包

3.运行main 方法 console控制台显示

spring boot 配置和启动_第2张图片

4.浏览器输入http://localhost:8080/test

spring boot 配置和启动_第3张图片

你可能感兴趣的:(JAVA,spring,boot)