spring boot 学习手册
  • 介绍
  • 基础教程
    • 1.RESTfull API简单项目的快速搭建
    • 2.配置文件详解:Properties和YAML
    • 3.配置文件-多环境配置
    • 4.日志配置-logback和log4j2
  • web应用开发
    • 1.模板引擎
    • 2.模板引擎FreeMarker
    • 3.模板引擎Thymeleaf
    • 4.模板引擎jsp
    • 5.错误处理
    • 6.Servlets, Filters, listeners
    • 7.CORS支持
    • 8.文件上传
    • 9.Interceptor拦截器
    • 10.AOP统一处理Web请求日志
    • 11.全局统一异常处理
    • 12. Retry重试
    • 13.RestTemplate
  • 关系型数据库
    • 1.JdbcTemplate
    • 2.Spring-data-jpa
      • @Id 和 @GeneratedValue 详解
      • 配置与注解备注
      • 全局统一前缀策略
      • 映射关系详解
      • 查询、分页、排序
      • JPA 资料
      • 进阶:jpa自定义行为方法
      • 进阶:锁
      • 进阶:Hibernate Validator
    • 3.事务处理
  • NoSQL数据库
    • 1.Redis
    • 2.Mongodb
  • Cache
    • 1.EhCache
    • 2.Redis
  • 异步消息服务
    • 1.JMS(ActiveMQ)
    • 2.AMQP(RabbitMQ)
  • Mybatis
    • Mybatis 初使用
    • Mybatis环境搭建
    • Mybatis开发流程
    • 业务开发流程
    • Mybatis资料
  • 进阶
    • 1.调用REST服务-使用代理
    • 2.发送邮件
    • 3.Spring Session实现集群-redis
    • 4.如何进行远程调试
    • 5.生产准备-基于HTTP的监控
    • 6.Spring Boot集成mybatis
    • 7.Spring Boot集成Druid
    • 8.Spring Boot集成Swagger
    • 9.生产部署-注意事项和如何使用脚本
  • 升华
    • Jenkins部署Spring Boot
    • 异步处理Http请求
    • FastDFS
    • Docker
    • 定时任务(corn job)
    • 批处理
    • @Async实现异步调用
  • 单元测试
    • WireMock伪造服务
  • 安全
    • 1.Spring Security
      • 认证
    • 2.Apache Shiro
  • TaskExecutor 异步线程池
  • 其他
    • 1.修改启动时显示
    • 2.获取配置文件中的值
    • 3.嵌入式容器
    • 4.配置SSL
    • 5.websocket
    • 6.Spring IO Platform
  • RESTfull API 开发
  • 附录:Eclipse - Spring Tool Suite工具的安装
  • 附录:Eclipse部署Maven
  • 附录:SpringBoot相关模块
  • 附录:注解笔记
  • 资料
  • 开发技巧
  • maven插件
Powered by GitBook
On this page
  • ①打开idea,选择Create New Project
  • ②项目配置,选择Spring Initializr,Project SDK选择1.8,URL选择默认,不做修改。如下图所示,然后选择Next
  • ④Spring Boot 版本选择,我这里选择了2.0.2,只要勾选Web下的web就可以了,然后Next
  • ⑤项目名称和路径,填写完后Finish
  • ⑤最后的项目结构
  • ⑥接下来就新建一个rest的Controller
  • ⑦运行启动即可,spring boot中,集成了tomcat,默认运行在8080端口
  • 继续编写IndexController 类,返回Map类型
  • 继续编写,返回一个对象
  • 编写测试类
  • 项目打包
  • 使用maven命令打包
  • 资料

Was this helpful?

  1. 基础教程

1.RESTfull API简单项目的快速搭建

Previous基础教程Next2.配置文件详解:Properties和YAML

Last updated 5 years ago

Was this helpful?

①打开idea,选择Create New Project

②项目配置,选择Spring Initializr,Project SDK选择1.8,URL选择默认,不做修改。如下图所示,然后选择Next

③继续项目配置

这里基本都已经自动生成了,简单介绍下:

  • Name:项目名称

  • Type:我们是Maven构建的,那么选择第一个Maven Project

  • Packaging:打包类型,打包成Jar文件

  • Java Version:jdk版本,选择1.8

  • Language:开发语言,选择Java

  • Group:对应pom文件中的groupId,项目组织的唯一标识,对应Java包的结构

  • Artifact:对应pom文件的artifactId,项目唯一标识,对应项目名称

  • Version:项目版本,对应pom文件的version

  • Description:项目描述,对应pom文件的description

  • Package:包名

④Spring Boot 版本选择,我这里选择了2.0.2,只要勾选Web下的web就可以了,然后Next

⑤项目名称和路径,填写完后Finish

⑤最后的项目结构

⑥接下来就新建一个rest的Controller

⑦运行启动即可,spring boot中,集成了tomcat,默认运行在8080端口

继续编写IndexController 类,返回Map类型

package com.shuju.example.controller;

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

import java.util.HashMap;
import java.util.Map;

@RestController
public class IndexController {

    @RequestMapping(value = "index")
    public String index()
    {
        return "hello world!";
    }

    @RequestMapping(value = "get")
    public Map<String, String> get(@RequestParam  String name) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", name);
        map.put("value", "hello world!");
        return map;
    }

}

你会发现直接将我们返回的map格式转成了json

继续编写,返回一个对象

新建一个bean目录,并创建User.java

IndexController中加入新方法

 @RequestMapping(value = "find/{id}/{name}")
public User get(@PathVariable int id, @PathVariable String name) {
    User user = new User();
    user.setId(id);
    user.setName(name);
    user.setDate(new Date());
    return user;
}

浏览器中访问

自动将对象转成json返回

编写测试类

模拟请求index路径方法

package com.shuju.example;

import com.shuju.example.controller.IndexController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    private MockMvc mvc;

    @Before
    public void before(){
        this.mvc = MockMvcBuilders.standaloneSetup(new IndexController()).build();
    }

    @Test
    public void contextLoads() throws Exception {

        // 模拟访问 index 请求
        RequestBuilder req = get("/index");
        mvc.perform(req).andExpect(status().isOk()).andExpect(content().string("hello world!"));

    }

}

右键运行,完成测试,如不正确会抛出异常。

项目打包

这里介绍一个工具,pom.xml加入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

很有用:

1、可以每次修改完保存,自动进行编译

2、打包时可以把依赖包自动打入

注意:

打包的我这里使用Maven插件,pom.xml中加入插件

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

使用maven命令打包

mvn clean compile //清除之前target编译文件并重新编译
mvn clean package //对项目进行打包(因为配置过插件,所以jar包是可执行的)
mvn clean install //安装项目,然后就可以使用了

可以通过自带的maven管理工具代替执行上面的命令

进行完package打包后,项目目录a 出现了target目录

其中 demo-0.0.1-SNAPSHOT.jar 就是打包 后的jar包,包括依赖包

直接单独运行测试

sudo java -jar demo-0.0.1-SNAPSHOT.jar

资料

补充:项目打包

项目打包时,动态设置参数,如端口号:

sudo java -jar demo-0.0.1-SNAPSHOT.jar --server.port=8888

嫌太长server.port,可以再这样设置,意思是$port如果存在,则使用,不存在则使用默认值8070

server:
  port: {$port:8070}

启动使用参数

sudo java -jar demo-0.0.1-SNAPSHOT.jar --server.port=8888

访问:

(devtool 是重新打包,局部单个文件的热部署可以使用这个)

访问访问测试:

http://localhost:8080/get?name=aaa
Intellij热部署插件JRebel
Intellij IDEA 使用Spring-boot-devTools无效解决办法
http://localhost:8080/get?name=aaa
IntelliJ IDEA搭建SpringBoot的小Demo
Intellij IDEA 打包jar的多种方式