关键词:
一、导入依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework</groupId> <artifactId>gs-actuator-service</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
二、构建实体
package hello;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
三、编写Controller
package hello; import java.util.concurrent.atomic.AtomicLong; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloWorldController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); @GetMapping("/hello-world") @ResponseBody public Greeting sayHello(@RequestParam(name="name", required=false, defaultValue="Stranger") String name) { return new Greeting(counter.incrementAndGet(), String.format(template, name)); } }
四、编写配置文件
server.port: 9000
management.server.port: 9001
management.server.address: 127.0.0.1
五、编写启动类
package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 使用Spring Boot Actuator构建RESTful Web服务 * */ @SpringBootApplication public class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); } }
六、编写测试类
/* * Copyright 2012-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package hello; import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.BDDAssertions.then; /** * Basic integration tests for service demo application. * * @author Dave Syer */ @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @TestPropertySource(properties = {"management.port=0"}) public class HelloWorldApplicationTests { @org.springframework.boot.context.embedded.LocalServerPort private int port; @Value("${local.management.port}") private int mgt; @Autowired private TestRestTemplate testRestTemplate; @Test public void shouldReturn200WhenSendingRequestToController() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity<Map> entity = this.testRestTemplate.getForEntity( "http://localhost:" + this.port + "/hello-world", Map.class); then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); } @Test public void shouldReturn200WhenSendingRequestToManagementEndpoint() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity<Map> entity = this.testRestTemplate.getForEntity( "http://localhost:" + this.mgt + "/actuator/info", Map.class); then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); } }
springboot实战之thymeleaf
...共同的职能是MVC模式中的视图展示层,即View。当然了,SpringBoot中也可以用jsp,不过不推荐这种用法,比较推崇的就是使用Thymeleaf。关于Thymeleaf学习,建议参考官方文档:https://www.thymeleaf.org/documentation.html官方文档例子,应有尽有。... 查看详情
springboot实战之使用springbootactuator构建restfulweb服务
一、导入依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.o 查看详情
springboot2.x实战之定时任务调度
...务的,例如:定时同步一批数据、定时清理一些数据,在SpringBoot中提供了@Scheduled注解就提供了定时调度的功能,对于简单的、单机的调度方案是足够了的。这篇文章准备用实际案例看下@Scheduled的用法。开发实战新建SpringBoot工... 查看详情
springboot2.x实战之定时任务调度
...务的,例如:定时同步一批数据、定时清理一些数据,在SpringBoot中提供了@Scheduled注解就提供了定时调度的功能,对于简单的、单机的调度方案是足够了的。这篇文章准备用实际案例看下@Scheduled的用法。开发实战新建SpringBoot工... 查看详情
springboot实战之接口日志篇
在本篇文章中不会详细介绍日志如何配置、如果切换另外一种日志工具之类的内容,只用于记录作者本人在工作过程中对日志的几种处理方式。1.Debug日志管理在开发的过程中,总会遇到各种莫名其妙的问题,而这些问题的定位... 查看详情
springboot实战系列之@value注解的使用心得
参考技术A在工作中使用springboot经常有属性注入的场景,下面说一下有默认值和无默认值两种写法的不同这中是有默认值的写法,默认是分号后的值,这里为true,但是如果在配置文件中(application.properties或application.yml)中设置了app... 查看详情
springboot实战(十三)之缓存
什么是缓存?引用下百度百科的解释:缓存就是数据交换的缓冲区(又称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,找到了则直接执行,找不到的话则从内存中查找。由于缓存的运行速度比内存快得多,故... 查看详情
springboot整合rabbitmq之发送接收消息实战
实战前言前几篇文章中,我们介绍了SpringBoot整合RabbitMQ的配置以及实战了Spring的事件驱动模型,这两篇文章对于我们后续实战RabbitMQ其他知识要点将起到奠基的作用的。特别是Spring的事件驱动模型,当我们全篇实战完毕RabbitMQ并... 查看详情
springboot+vue+实战项目之第1集(代码片段)
SpringBoot+vue+实战项目--锋迷商城1.项目功能2.技术选型3.项目架构的演进4.SpringBoot5.Usercontroller(实操注册用户)6.Java配置方式6.1xml配置6.2注解配置6.3Java配置⽅式(第三方类对象)7.SpringBoot自动配置(底层... 查看详情
springboot实战之使用jdbc和spring访问数据库
这里演示的是h2databse示例,所以简单的介绍普及下h2database相关知识H2数据库是一个开源的关系型数据库。H2是一个嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提供了一个十分方便的web控制台用于操作和管理数... 查看详情
springboot实战之使用jdbc和spring访问数据库
这里演示的是h2databse示例,所以简单的介绍普及下h2database相关知识H2数据库是一个开源的关系型数据库。H2是一个嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提供了一个十分方便的web控制台用于操作和管理数... 查看详情
springboot实战之url传参
参考技术A其中required=false是说明这个字段可以传入也可以不传入,如果不这样写,就会匹配version字段,匹配不到,就会报错,这时,如果访问"/index"路径也会报错。【注意】要点击@PathVariable注解进去看一下,是否支持required参数... 查看详情
springboot监控管理之admin监管使用
SpringBootAdmin用于监控基于SpringBoot的应用,它是在SpringBootActuator的基础上提供简洁的可视化WEBUI。SpringBootAdmin是一个社区项目,用于管理和监视SpringBoot®应用程序。其实说作用大也大,说不大也不大。感兴趣的同学可以了解一下... 查看详情
《02.springboot连载:springboot实战.springboot核心原理剖析》
在上节中我们通过了一个小的入门案例已经看到了SpringBoot的强大和简单之处。本章将详细介绍SpringBoot的核心注解,基本配置和运行机制。笔者一直认为:精通一个技术一定要深入了解这个技术帮助我们做了哪些动作,深入理解它... 查看详情
springboot2.0实战(23)整合redis之集成缓存springdatacache
SpringBoot2.0实战(23)整合Redis之集成缓存SpringDataCache来源:https://www.toutiao.com/a6803168397090619908/?timestamp=1584450221&app=news_article_lite&group_id=6803168397090619908&req_id=20200317210341 查看详情
springboot实战系列之完整参数校验案例
...会进入controller的方法体中,即没有走校验;传会走校验springboot关于controller层传递单个参数的校验JavaBeanValidation校验@PathVariable和@RequestParam解决办法:@ExceptionHandlervalue中的异常类要和方法体重的参数的异常类相同或者是其父类,... 查看详情
springboot实战实现分布式锁一之重现多线程高并发场景
实战前言:上篇博文我总体介绍了我这套视频课程:“SpringBoot实战实现分布式锁”总体涉及的内容,从本篇文章开始,我将开始介绍其中涉及到的相关知识要点,感兴趣的小伙伴可以关注关注学习学习!!工欲善其事,必先利... 查看详情
springboot实战之rabbitmq
什么是RabbitMQ?RabbitMQ是一个消息代理。它的核心原理非常简单:接收和发送消息。你可以把它想像成一个邮局:你把信件放入邮箱,邮递员就会把信件投递到你的收件人处。在这个比喻中,RabbitMQ就扮演着邮箱、邮局以及邮递员... 查看详情