spring boot 加载顺序boot单元测试的时候,怎么才能只加载

SpringBoot单元测试 | IceKredit
Spring Boot Test
Spring Boot提供了一些注解和工具去帮助开发者测试他们的应用。相较于SpringBoot1.3,SpringBoot1.4对测试有了大的改进,以下示例适用SpringBoot1.4.1以及以上版本。在项目中使用Spring Boot Test支持,只需要在pom.xml引入如下配置即可:
&dependency&
&groupId&org.springframework.boot&/groupId&
&artifactId&spring-boot-starter-test&/artifactId&
&scope&test&/scope&
&/dependency&
spring-boot-start-test
SpringBoot提供了spring-boot-start-test启动器,该启动器提供了常见的单元测试库:
: 一个Java语言的单元测试框架
& Spring Boot Test:为Spring Boot应用提供集成测试和工具支持
:支持流式断言的Java测试框架
:一个匹配器库
:一个java mock框架
:一个针对JSON的断言库
:JSON XPath库
2. 常用注解
这里介绍一些Spring Boot单元测试常用的注解,更多详细请到Spring Boot官网。
@RunWith(SpringRunner.class)
JUnit运行使用Spring的测试支持。SpringRunner是SpringJUnit4ClassRunner的新名字,这样做的目的
仅仅是为了让名字看起来更简单一点。
@SpringBootTest
该注解为SpringApplication创建上下文并支持Spring Boot特性,其webEnvironment提供如下配置:
Mock-加载WebApplicationContext并提供Mock Servlet环境,嵌入的Servlet容器不会被启动。
RANDOM_PORT-加载一个EmbeddedWebApplicationContext并提供一个真实的servlet环境。嵌入的Servlet容器将被启动并在一个随机端口上监听。
DEFINED_PORT-加载一个EmbeddedWebApplicationContext并提供一个真实的servlet环境。嵌入的Servlet容器将被启动并在一个默认的端口上监听
(application.properties配置端口或者默认端口8080)。
NONE-使用SpringApplication加载一个ApplicationContext,但是不提供任何的servlet环境。
在你的ApplicationContext里为一个bean定义一个Mockito mock。
定制化Mock某些方法。使用@SpyBean除了被打过桩的函数,其它的函数都将真实返回。
@WebMvcTest
该注解被限制为一个单一的controller,需要利用@MockBean去Mock合作者(如service)。
测试用例设计
1. 测试用例设计方法
根据目前现状,单元测试主要用来进行程序核心逻辑测试。逻辑覆盖测试是通过对程序逻辑结构的遍历来实现程序逻辑覆盖。从对源代码的覆盖程度不同分为以下六种标准,本文只对其中的五种进行分析(路径覆盖除外),下面从一段代码开始。
public int example(int x, int y, int z){
if (x&1 && z&2){
if (y == 3 || x & 5){
x = x - 2;
一般单元测试不会根据代码来写用例,而是会根据流程图来编写测试用例,以上代码画出的流程图如下:
设计足够多的测试用例,使得被测试程序中的每条可执行语句至少被执行一次。
2. 测试用例
{x=6;y=3;z=3}
a->c->b->d->e->f
3. 测试的充分性
假设语句x1&&z&2中的&&写成了||上面的测试用例是检查不出来的。
设计足够的测试用例使得代码中的判断真、假分支至少被执行一次。我们标记x&1&&z&2 为P1 y==3 || x&5为P2。
2. 测试用例
{x=3;y=3;z=3}
a->c->b->d->e->f
{x=0;y=2;z=3}
a->c->d->f
3. 测试的充分性
假设语句y==3 || x&5中的||写成了&&上面的测试用例是检查不出来的。和语句覆盖相比:由于判定覆盖不是在判断假分支就是在判断真分支,所以满足了判定覆盖就一定会满足语句覆盖。
设计足够多的测试用例,使得被测试程序每个判断语句中的每个逻辑条件的可能值至少满足一次。在本例中有两个判断分支(x&1&&z&2)和(y == 3 || x & 5)分别记为P1和P2。总共有三个条件x&1、z&2、y==3和x&5分别记为B1、B2、B3、B4。
2. 测试用例
{x=0;y=2;z=3}
a->c->d->f
{x=3;y=3;z=1}
a->c->d->f
3. 测试的充分性
从上面的结论看,条件覆盖没法满足100%的语句覆盖,当然没法满足100%的判定覆盖。
判定/条件覆盖
同时满足100%的条件覆盖和100%的判定覆盖。
2. 测试用例
{x=0;y=2;z=1}
a->c->d->f
{x=3;y=3;z=3}
a->c->b->d->e->f
3. 测试的充分性
达到100%判定-条件覆盖标准一定能够达到100%条件覆盖、100%判定覆盖和100%语句覆盖。
条件组合覆盖
设计足够多的测试用例,使得被测试程序中的每个判断的所有可能条件取值的组合至少被满足一次。
条件组合只针对同一个判断语句内存在多个判断条件,让这些条件的取值进行笛卡尔乘积组合。
不同判断语句内的条件无需组合
对于单条件语句,只需要满足自己的所有取值即可
本例中判断(x&1&&z&2)有如下组合:(1)x>1&&z>2 (2)x>1&&z&=2 (3)x&=1&&z>2 (4) x&=1&&z&=2;判断 (y == 3 || x & 5)有如下组合(1)y==3||x>5 (2)y==3||x&=5 (3)y!=3||x>5 (4)y!=3||x&=5
2. 测试用例
x>1 and z>2 y==3 or x>5
{x=3;z=3;y=3}
a->c->b->d->e->f
x>1 and z&=2 y==3 or x&=5
{x=2;z=3;y=3}
a->c->b->d->f
x&=1 and z>2 y!=3 or x>5
{x=1;z=3;y=5}
a->c->d->e->f
x&=1 and z&=2 y!=3 or x&=5
{x=1;z=2;y=3}
a->c->d->f
3. 测试的充分性
100%满足条件组合标准一定满足100%条件覆盖标准和100%判定覆盖标准。
在单元测试中替代Get和Post请求测试方式
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ScoreControllerTestNew {
@Autowired
private TestRestTemplate restT
public void testScore(){
String jsonStr = "{\"data\":{\"debit_account_balance_code\":40,\"credit_consume_count\":1,\"debit_start_age\":1,\"debit_consume_sum_code\":2,\"age\":38},\"modelProductId\":55}";
String content = this.restTemplate.postForObject("/scoreApi/score", jsonStr, String.class );
assertThat(content).isEqualTo("{\"result\":{\"score\":\"300\",\"logit\":21.997},\"response_code\":\"00\",\"response_msg\":\"success\"}");
测试Controller
在测试Controller时需要进行隔离测试,这个时候需要Mock Service层的服务。
@RunWith(SpringRunner.class)
@WebMvcTest(ScoreController.class)
public class ScoreControllerTestNew {
@Autowired
private MockMvc mockM
private ICalculateService calculateS
private IModelMonitorService modelMonitorS
private IScoreConfigService scoreConfigS
private IModelProductService modelProductS
public void setUp(){
public void testScore() throws Exception {
given(this.modelProductService.get(anyLong()))
.willReturn(null);
String jsonStr = "{\"data\":{\"debit_account_balance_code\":40,\"credit_consume_count\":1,\"debit_start_age\":1,\"debit_consume_sum_code\":2,\"age\":38},\"modelProductId\":5}";
RequestBuilder requestBuilder =
requestBuilder = post("/scoreApi/score").contentType(MediaType.APPLICATION_JSON).content(jsonStr);
this.mockMvc.perform(requestBuilder).andExpect(status().isOk()).andExpect(MockMvcResultMatchers.content().string("{}"));
测试Service
测试Service和测试Controller类似,同样采用隔离法。
@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTest {
private ModelMonitorMapper modelMonitorM
@Autowired
private IModelMonitorService modelServiceServiceI
public void testModelServiceServiceImpl(){
given(modelMonitorMapper.insert(anyObject()))
.willReturn(0);
modelServiceServiceImpl.insert(new ModelMonitor());
assertThat(n).isEqualTo(0);
测试的时候为了防止引入脏数据使用注解@Transactional和@Rollback在测试完成后进行回滚。
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ScoreControllerTestNew {
@Autowired
private ModelMonitorMapper modelMonitorM
public void testDao() throws Exception {
ModelMonitor modelMonitor = new ModelMonitor();
modelMonitor.setModelProductId(Long.parseLong("5"));
modelMonitor.setLogit(21.997);
modelMonitor.setDerivedVariables("{\"debit_account_balance_code\":1.0,\"credit_consume_count\":1.0,\"debit_start_age\":1.0,\"debit_consume_sum_code\":1.0,\"age\":1.0}");
modelMonitor.setScore("300");
modelMonitor.setSrcData("{\"data\":{\"debit_account_balance_code\":40,\"credit_consume_count\":1,\"debit_start_age\":1,\"debit_consume_sum_code\":2,\"age\":38},\"modelProductId\":5}");
int n = modelMonitorMapper.insert(modelMonitor);
assertThat(n).as("检查数据是否成功插入").isEqualTo(0);
本条目发布于。属于分类。作者是。在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
标签:至少1个,最多5个
单元测试对于开发人员来说是非常熟悉的,我们每天的工作也都是围绕着开发与测试进行的,在最早的时候测试都是采用工具Debug模式进行调试程序,后来Junit的诞生也让程序测试发生了很大的变化。我们今天来讲解下基于SpringBoot结合Junit怎么来完成单元测试。
基于SpringBoot平台整合Junit分别完成客户端、服务端的单元测试。
我们首先使用idea工具创建一个SpringBoot项目,并且添加相关Web、MySQL、JPA依赖,具体pom.xml配置依赖内容如下所示:
.../省略其他配置
&dependencies&
&!--web依赖--&
&dependency&
&groupId&org.springframework.boot&/groupId&
&artifactId&spring-boot-starter-web&/artifactId&
&/dependency&
&!--data jpa依赖--&
&dependency&
&groupId&org.springframework.boot&/groupId&
&artifactId&spring-boot-starter-data-jpa&/artifactId&
&/dependency&
&!--druid数据源依赖--&
&dependency&
&groupId&com.alibaba&/groupId&
&artifactId&druid&/artifactId&
&version&1.0.31&/version&
&/dependency&
&!--lombok依赖--&
&dependency&
&groupId&org.projectlombok&/groupId&
&artifactId&lombok&/artifactId&
&/dependency&
&!--MySQL依赖--&
&dependency&
&groupId&mysql&/groupId&
&artifactId&mysql-connector-java&/artifactId&
&/dependency&
&!--springboot程序测试依赖,创建项目默认添加--&
&dependency&
&groupId&org.springframework.boot&/groupId&
&artifactId&spring-boot-starter-test&/artifactId&
&scope&test&/scope&
&/dependency&
&/dependencies&
.../省略其他配置
配置数据库
我们本章的内容需要访问数据库,我们先在src/main/resources下添加application.yml配置文件,对应添加数据库配置信息如下所示:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8
username: root
password: 123456
#最大活跃数
maxActive: 20
#初始化数量
initialSize: 1
#最大连接等待超时时间
maxWait: 60000
#打开PSCache,并且指定每个连接PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
#通过connectionProperties属性来打开mergeSql功能;慢SQL记录
#connectionProperties: druid.stat.mergeSql=druid.stat.slowSqlMillis=5000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 1 from dual
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
#配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
filters: stat, wall, log4j
properties:
hibernate:
show_sql: true
format_sql: true
以上配置都是比较常用到,这里不做多解释了,如果不明白可以去本文底部SpringBoot学习目录文章内找寻对应的章节。
对应数据库内的数据表来创建一个商品基本信息实体,实体内容如下所示:
package com.yuqiyu.chapter35.
import lombok.D
import javax.persistence.*;
import java.io.S
* 商品基本信息实体
* ========================
* Created with IntelliJ IDEA.
* User:恒宇少年
* Time:22:20
* 码云:http://git.oschina.net/jnyqy
* ========================
@Table(name = "good_infos")
public class GoodInfoEntity implements Serializable
//商品编号
@Column(name = "tg_id")
@GeneratedValue
private Integer tgId;
//商品类型编号
@Column(name = "tg_type_id")
private Integer typeId;
//商品标题
@Column(name = "tg_title")
//商品价格
@Column(name = "tg_price")
//商品排序
@Column(name = "tg_order")
基于商品基本信息实体类创建一个JPA接口,该接口继承JpaRepository接口完成框架通过反向代理模式进行生成实现类,自定义JPA接口内容如下所示:
package com.yuqiyu.chapter35.
import com.yuqiyu.chapter35.bean.GoodInfoE
import org.springframework.data.jpa.repository.JpaR
* ========================
* Created with IntelliJ IDEA.
* User:恒宇少年
* Time:22:23
* 码云:http://git.oschina.net/jnyqy
* ========================
public interface GoodInfoJPA
extends JpaRepository&GoodInfoEntity,Integer&
构建测试控制器
下面我们开始为单元测试来做准备工作,先来创建一个SpringMVC控制器来处理请求,代码如下所示:
package com.yuqiyu.chapter35.
import com.yuqiyu.chapter35.bean.GoodInfoE
import com.yuqiyu.chapter35.jpa.GoodInfoJPA;
import org.springframework.beans.factory.annotation.A
import org.springframework.web.bind.annotation.RequestM
import org.springframework.web.bind.annotation.RequestM
import org.springframework.web.bind.annotation.RestC
import java.util.L
* ===============================
* Created with Eclipse.
* User:于起宇
* Time:18:37
* 简书:http://www.jianshu.com/u/092df3f77bca
* ================================
@RestController
public class TestController
//商品基本信息数据接口
@Autowired
private GoodInfoJPA goodInfoJPA;
* 查询首页内容
@RequestMapping(value = "/index")
public String index(String name)
return "this is index page" +
* 查询全部商品
@RequestMapping(value = "/all")
public List&GoodInfoEntity& selectAll()
return goodInfoJPA.findAll();
* 查询商品详情
* @param goodId
@RequestMapping(value = "/detail",method = RequestMethod.GET)
public GoodInfoEntity selectOne(Integer goodId)
return goodInfoJPA.findOne(goodId);
我们在测试控制内注入了GoodInfoJPA,获得了操作商品基本信息的数据接口代理实例,我们可以通过该代理实例去做一些数据库操作,如上代码selectAll、detail方法所示。在测试控制器内添加了三个测试MVC方法,我们接下来开始编写单元测试代码。
编写单元测试
在我们使用idea开发工具构建完成SpringBoot项目后,会自动为我们添加spring-boot-starter-test依赖到pom.xml配置文件内,当然也为我们自动创建了一个测试类,该类内一开始是没有过多的代码的。下面我们开始基于该测试类进行添加逻辑,代码如下所示:
....//省略依赖导包
* 单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter35ApplicationTests {
* 模拟mvc测试对象
private MockMvc mockM
* web项目上下文
@Autowired
private WebApplicationContext webApplicationC
* 商品业务数据接口
@Autowired
private GoodInfoJPA goodInfoJPA;
* 所有测试方法执行之前执行该方法
public void before() {
//获取mockmvc对象实例
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
在上面测试代码中我们从上面开始讲解下,其中@RunWith这里就不多做解释了,我们最比较常用到的就是这个注解。
@SpringBootTest这个注解这里要强调下,这是SpringBoot项目测试的核心注解,标识该测试类以SpringBoot方式运行,该注解的源码如下所示:
...//省略导包
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
public @interface SpringBootTest {
@AliasFor("properties")
String[] value() default {};
@AliasFor("value")
String[] properties() default {};
Class&?&[] classes() default {};
SpringBootTest.WebEnvironment webEnvironment() default SpringBootTest.WebEnvironment.MOCK;
public static enum WebEnvironment {
MOCK(false),
RANDOM_PORT(true),
DEFINED_PORT(true),
NONE(false);
private fi
private WebEnvironment(boolean embedded) {
this.embedded =
public boolean isEmbedded() {
return this.
我们可以看到在@SpringBootTest注解源码中最为重要的就是@BootstrapWith,该注解才是配置了测试类的启动方式,以及启动时使用实现类的类型。
测试index请求
MockMvc这个类是一个被final修饰的类型,该类无法被继承使用。这个类是Spring为我们提供模拟SpringMVC请求的实例类,该类则是由MockMvcBuilders通过WebApplicationContext实例进行创建的,初始化MockMvc实例我们可以看下before方法逻辑。到现在为止我们才是万事俱备就差编写单元测试逻辑了,我们首先来编写访问/index请求路径的测试,具体测试代码如下所示:
* 测试访问/index地址
* @throws Exception
public void testIndex() throws Exception {
MvcResult mvcResult = mockMvc
.perform(// 1
MockMvcRequestBuilders.get("/index") // 2
.param("name","admin") // 3
.andReturn();// 4
int status = mvcResult.getResponse().getStatus(); // 5
String responseString = mvcResult.getResponse().getContentAsString(); // 6
Assert.assertEquals("请求错误", 200, status); // 7
Assert.assertEquals("返回结果不一致", "this is index pageadmin", responseString); // 8
MockMvc解析
我在上面代码中进行了标记,我们按照标记进行讲解,这样会更明白一些:1 perform方法其实只是为了构建一个请求,并且返回ResultActions实例,该实例则是可以获取到请求的返回内容。2 MockMvcRequestBuilders该抽象类则是可以构建多种请求方式,如:Post、Get、Put、Delete等常用的请求方式,其中参数则是我们需要请求的本项目的相对路径,/则是项目请求的根路径。3 param方法用于在发送请求时携带参数,当然除了该方法还有很多其他的方法,大家可以根据实际请求情况选择调用。4 andReturn方法则是在发送请求后需要获取放回时调用,该方法返回MvcResult 对象,该对象可以获取到返回的视图名称、返回的Response状态、获取拦截请求的拦截器集合等。5 我们在这里就是使用到了第4步内的MvcResult对象实例获取的MockHttpServletResponse对象从而才得到的Status状态码。6 同样也是使用MvcResult实例获取的MockHttpServletResponse对象从而得到的请求返回的字符串内容。【可以查看rest返回的json数据】7 使用Junit内部验证类Assert判断返回的状态码是否正常为2008 判断返回的字符串是否与我们预计的一样。
测试商品详情
直接上代码吧,跟上面的代码几乎一致,如下所示:
* 测试查询详情
* @throws Exception
public void testDetail() throws Exception
MvcResult mvcResult = mockMvc
MockMvcRequestBuilders.get("/detail")
.param("goodId","2")
.andReturn(); // 5
//输出经历的拦截器
HandlerInterceptor[] interceptors = mvcResult.getInterceptors();
System.out.println(interceptors[0].getClass().getName());
int status = mvcResult.getResponse().getStatus(); // 6
String responseString = mvcResult.getResponse().getContentAsString(); // 7
System.out.println("返回内容:"+responseString);
Assert.assertEquals("return status not equals 200", 200, status); // 8
上面唯一一个部分需要解释下,在上面测试方法内输出了请求经历的拦截器,如果我们配置了多个拦截器这里会根据先后顺序写入到拦截器数组内,其他的MockMvc测试方法以及参数跟上面测试方法一致。
在测试类声明定义全局字段时,我们注入了GoodInfoJPA实例,当然单元测试也不仅仅是客户端也就是使用MockMvc方式进行的,我们也可以直接调用JPA、Service进行直接测试。下面我们来测试下商品基本信息的添加,代码如下所示:
* 测试添加商品基本信息
public void testInsert()
* 商品基本信息实体
GoodInfoEntity goodInfoEntity = new GoodInfoEntity();
goodInfoEntity.setTitle("西红柿");
goodInfoEntity.setOrder(2);
goodInfoEntity.setPrice(5.82);
goodInfoEntity.setTypeId(1);
goodInfoJPA.save(goodInfoEntity);
* 测试是否添加成功
* 验证主键是否存在
Assert.assertNotNull(goodInfoEntity.getTgId());
在上面代码中并没有什么特殊的部分,是我们在使用Data JPA时用到的save方法用于执行添加,在添加完成后验证主键的值是否存在,NotNull时证明添加成功。
与添加差别不大,代码如下所示:
* 测试删除商品基本信息
public void testDelete()
//根据主键删除
goodInfoJPA.delete(3);
//验证数据库是否已经删除
Assert.assertNull(goodInfoJPA.findOne(3));
在上面代码中,我们根据主键的值进行删除商品的基本信息,执行删除完成后调用selectOne方法查看数据库内是否已经不存在该条数据了。
本章主要介绍了基于SpringBoot平台的两种单元测试方式,一种是在服务端采用Spring注入方式将需要测试的JPA或者Service注入到测试类中,然后调用方法即可。另外一种则是在客户端采用MockMvc方式测试Web请求,根据传递的不用参数以及请求返回对象反馈信息进行验证测试。
本章代码已经上传到码云:网页地址:Git地址:SpringBoot相关系列文章请访问:QueryDSL相关系列文章请访问:SpringDataJPA相关系列文章请访问:感谢阅读!欢迎加入QQ技术交流群,共同进步。
0 收藏&&|&&0
你可能感兴趣的文章
21 收藏,4.8k
106 收藏,2.3k
2 收藏,1.6k
分享到微博?
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。spring boot 怎么加载controller_百度知道
spring boot 怎么加载controller
我有更好的答案
页面位置 /WEB-INF/jsp/page// 页面位置 /@Controllerpublic class PageController {
//jsp&#47.tomcat.embed&page/page1&& 直接返回字符串,框架默认会去 spring./groupId&WEB-INF/message&.
&lt.annotation.Vimport org.springframework.web.servlet.ModelAndV
&#47.time&quot.addObject(&quot.annotation.RequestMapping.hello);
return &quot.put(&page2&)
public String page2(Model model){
&#47.put(&import org.springframework.dependency&
* 默认页&br&#47,直接返回页面字符串)
* @author SHANHY
@RequestMapping(&/artifactId&**
* 响应到JSP页面page1
* @author SHANHY
@RequestMapping(&/page1&; model){
&tomcat-embed-jasper&/dependency&&
* 响应到JSP页面page1(可以直接使用Model封装内容;import org.springframework.web.
&lt.stereotype.Controller,如取不到默认值为Hello Shanhy
@Value(&${application.”创建PageController.util.DartifactId&/artifactId&
&/dependency&上面说了spring-boot 不推荐JSP,想使用JSP需要配置application.properties。 添加src/main/resources/application.properties内容:# 页面默认前缀目录spring.mvc.view.prefix=/WEB-INF/jsp/# 响应页面默认后缀spring.mvc.view.suffix=.jsp# 自定义属性,可以在Controller中读取application.hello=Hello Shanhy123456在 src/main 下面创建 webapp/WEB-INF/jsp 目录用来存放我们的jsp页面。 index.jsp&%@ page language=&java& pageEncoding=&UTF-8&%&&!DOCTYPE HTML PUBLIC &-//W3C//DTD HTML 4.01 Transitional//EN&&&html&&head&&title&Spring Boot Sample&/title&&/head&&body&
Time: ${time}
Message: ${message}&/body&&/html&11213page1.jsp&%@ page language=&java& pageEncoding=&UTF-8&%&&!DOCTYPE HTML PUBLIC &-//W3C//DTD HTML 4.01 Transitional//EN&&&html&&head&&title&Spring Boot Sample&/title&&/head&&body&
&h1&${content }&/h1&&/body&&/html&1要想让spring-boot支持JSP,需要将项目打成war包。 我们做最后一点修改,修改pom.xml文件,将 jar 中的 jar 修改为 war然后启动spring-boot服务。;jstl&);/groupId&gt, this, hello);index&;page.jsp
ModelAndView mav = new ModelAndView(&
public String index(Map&/ 本例为 /WEB-INF/jsp/index.jsp
return &page&#47.prefix 目录下的 (index拼接spring.content&;&groupId&javax.servlet&lt.Mcontent&;);) 和 @RequestMapping 是有区别的
* 如果不写参数,则为全局默认页,加入输入404页面,也会自动访问到这个页面。
* 如果加了参数“/dependency&gt,则只认为是根页面。
* @author SHANHY
&import java.util.Mscope&
&&#47.suffix)页面
&#47,编码如下:package org.,&/page1&)
public ModelAndView page1(){
* @RequestMapping(&/, new Date());groupId&org.apache:Hello Shanhy}&)
private String hello = &Hello Shanhy&;artifactId&gt, hello + &(第二种)& 从 application.properties 中读取配置;
@RequestMapping(value = {&&#47.
}}566pom.xml添加依赖:
model.import java.jsp
model.addAttribute(&scope&provided&&#47.sample.String, Object&gt
资深电脑人
为您推荐:
其他类似问题
spring的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。}

我要回帖

更多关于 spring boot 热加载 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信