学习搭建SpringBoot项目(一)

因为一直想做javaweb开发所以最近学习一下怎么搭建一个SpringBoot的web项目并记录下来。

1 IntelliJ IDEA下载和Maven搭建

1 IntelliJ IDEA 官方下载地址
2 JDK 和 maven下载和环境配置
注:因为SpringBoot内置tomcat 所以不用下载配置

2 新建project 和配置 SpringBoot开发环境

1打开idea

new project → 选择Maven → Next


学习搭建SpringBoot项目(一)_第1张图片
创建新项目.png

2找到pom.xml文件 copy以下代码覆盖原有内容



    4.0.0
    com.lena
    demo
    1.0.0-SNAPSHOT
    jar
    demo
    Demo project for Spring Boot
    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.0.RELEASE
         
    
    
        UTF-8
        UTF-8
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

3更新下载 maven中的jar包

选择工程 右键 → Maven → Reimport

学习搭建SpringBoot项目(一)_第2张图片
更新maven中jar包配置.png

3 完善新建工程目录

学习搭建SpringBoot项目(一)_第3张图片
文件目录.png
application.properties中代码
#修改端口号8080改为8888
server.port=8888
MyApplication中代码
package example;

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);
    }
}
实体类MyInfo中代码
package example;

public class MyInfo {

    private long id;//主键.
    private String name;//测试名称.

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

JsonController 中代码
package example;

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

@RestController
public class JsonController {
    @RequestMapping("/info")
    public MyInfo info() {
        MyInfo info = new MyInfo();
        info.setId(1003220931);
        info.setName("LeeBoo");
        return info;
    }
}

菜单栏Run → Edit Configurations 打开下图窗口
Main class栏 选择Myapplication → 点击按钮Ok 和 Apply


学习搭建SpringBoot项目(一)_第4张图片
Paste_Image.png

到这里就可以运行项目了(用浏览器访问 注意端口号已经改为8888)

学习搭建SpringBoot项目(一)_第5张图片
Paste_Image.png

你可能感兴趣的:(学习搭建SpringBoot项目(一))