1、添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot2</groupId>
<artifactId>lesson1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lesson1</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build> </project>

  

2、配置application.properties

server.port=9090
server.servlet.context-path=/chapter1

  

3、编写rest接口

package com.springboot2.lesson1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@SpringBootApplication
public class Lesson1Application { public static void main(String[] args) {
SpringApplication.run(Lesson1Application.class, args);
} @GetMapping("/demo1")
public String demo1() {
return "Hello battcn";
} }

  

4、测试

package com.springboot2.lesson1;

import static org.junit.Assert.assertEquals;

import java.net.URL;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class Lesson1ApplicationTests { @LocalServerPort
private int port; private URL base; @Autowired
private TestRestTemplate template; @BeforeEach
public void setUp() throws Exception {
// TODO 因为我们修改了 content-path 所以请求后面要带上
this.base = new URL("http://localhost:" + port + "/chapter1/demo1");
} @Test
public void demo1() throws Exception {
ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
assertEquals(response.getBody(), "Hello battcn");
} }

  

1、springboot2新建web项目的更多相关文章

  1. 用Maven新建Web项目时报错

    在cmd下,用mvn命令 mvn archetype:create -DgroupId=org.seckill -DartifactId=seckill -DarchetypeArtifactId=m ...

  2. Eclipse下Maven新建Web项目index.jsp报错完美解决(war包)

    Eclipse下Maven新建Web项目步骤 1. 2. 3. 4. 5. 问题描述 最近用eclipse新建了一个maven项目,结果刚新建完成index.jsp页面就报错了,先把错误信息贴出来看看 ...

  3. IDEA旗舰版新建web项目

    即在一个Project下(MyEclipse中为工作空间)新建一个Module. 点击,在弹出框上打一个勾,如下图: 点Next,输入项目名,如下图: 点Finish, 右键WEB-INF,新建2个D ...

  4. 使用Intellij IDEA新建Web项目

    在学习Servlet的过程中,发现大多数的教程都是使用MyEclipse或者Eclipse来创建Web项目,这让一直使用高逼格的LZ很是不爽,于是自己配置了一下使用Intellij IDEA新建了We ...

  5. VS2013新建web项目时出错,系统找不到指定文件

    好不容易找了一个VS2013_RTM_ULT_CHS来安装好,Key:BWG7X-J98B3-W34RT-33B3R-JVYW9 兴高采烈的打开vs2013,新建web项目,结果弹出一个错误: 傻眼了 ...

  6. eclipse新建web项目,发布 run as 方式和 new server然后添加项目方式。 后者无法自动编译java 成class文件到classes包下。

    eclipse新建web项目,发布 run as 方式和 new server然后添加项目方式. 后者无法自动编译java 成class文件到classes包下. 建议使用run as  -  run ...

  7. maven新建web项目提示The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    maven新建web项目提示The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bu ...

  8. Eclipse java SE版本解决无法新建web项目问题

    最近工作要涉及web开发,之前下载的java SE (我的是indigo) 版本默认无法新建web项目,也就是找不到Dynamic Web ,在网上看了些解决办法,最终却是解决了问题,说到底就是安装一 ...

  9. Maven的环境搭建及新建web项目

    第一次接触maven,做一个简单的记录 一.下载maven及环境变量的配置 下载地址 http://maven.apache.org/download.cgi 配置其环境变量  MAVEN_HOME= ...

随机推荐

  1. 日常Bug排查-抛异常不回滚

    日常Bug排查-抛异常不回滚 前言 日常Bug排查系列都是一些简单Bug排查,笔者将在这里介绍一些排查Bug的简单技巧,同时顺便积累素材_. Bug现场 最近有人反映java应用操作数据库的时候,抛异 ...

  2. openresty 学习笔记三:连接redis和进行相关操作

    openresty 学习笔记三:连接redis和进行相关操作 openresty 因其非阻塞的调用,令服务器拥有高性能高并发,当涉及到数据库操作时,更应该选择有高速读写速度的redis进行数据处理.避 ...

  3. Echarts-2.2.7中统计出来的统计图保存为图片

    今天在做一个图形报表,有个需求是要把展现的统计图保存为图片, 图形报表用的Echarts-2.2.7, 以前有用过 Echarts,记得echarts插件是可以帮助我们把统计图保存为图片的. 只是不记 ...

  4. TVM代码生成codegen

    TVM代码生成codegen 硬件后端提供程序(例如Intel,NVIDIA,ARM等),提供诸如cuBLAS或cuDNN之类的内核库以及许多常用的深度学习内核,或者提供框架例,如带有图形引擎的DNN ...

  5. VAE变分自编码器实现

    变分自编码器(VAE)组合了神经网络和贝叶斯推理这两种最好的方法,是最酷的神经网络,已经成为无监督学习的流行方法之一. 变分自编码器是一个扭曲的自编码器.同自编码器的传统编码器和解码器网络一起,具有附 ...

  6. YOLOV4各个创新功能模块技术分析(三)

    YOLOV4各个创新功能模块技术分析(三)  八.数据增强相关-Stylized-ImageNet 论文名称:ImageNet-trained cnns are biased towards text ...

  7. Python 应用领域以及版本之间的区别

    Python 应用领域以及版本之间的区别 一.Python应用领域 1. Python+人工智能,给你更多研究方向选择! 2. 企业级综合实战项目,集六大前沿技术为一体 二. Python 2与Pyt ...

  8. centos 7查看系统网络情况netstat

    查看系统网络情况 netstat ➢ 基本语法 netstat [选项] ➢ 选项说明 -an 按一定顺序排列输出 -p 显示哪个进程在调用 应用案例 请查看服务名为 sshd 的服务的信息. ➢ N ...

  9. Spring自定义转换类,让@Value更方便

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 关于配置的文章已经写了很多,相信看过的人还是会有一定收获的,系列文章可阅读:南瓜慢说-配置相关文章.对于@Val ...

  10. NOIP模拟测试26「嚎叫响彻在贪婪的机房·主仆见证了 Hobo 的离别·征途堆积出友情的永恒」

    题目比较神仙,注意是题目神仙 贪婪暗示贪心,堆积暗示堆优化$\%\%\%\%\%\%\%$ 两个乱搞$+$一个堆优化$dp$ 嚎叫响彻在贪婪的机房 题解 对于一个序列来说只要他们差的$gcd$不为$1 ...