一、导入依赖

<?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);
} }

最新文章

  1. Python命名规范
  2. div+css遮罩层
  3. 使用poi将word转换为html
  4. Unity3d fur真实毛发渲染
  5. 【转】 Linux/Unix 进程间通信的各种方式及其比较
  6. linux之SQL语句简明教程---GROUP BY
  7. STL 源代码剖析 算法 stl_algo.h -- merge sort
  8. (Problem 28)Number spiral diagonals
  9. GSAP学习笔记
  10. ASP.NET Core官方资料入口
  11. 贪心+bfs 或者 并查集 Codeforces Round #268 (Div. 2) D
  12. ER图,以及转化成关系模式
  13. django - 总结 - cnblog
  14. AndrewNG Deep learning课程笔记 - CNN
  15. LabVIEW(十二):VI本地化-控件标题内容的修改
  16. OneZero第一次站立会议&amp;Sprint Planning Meeting(2016.3.21)
  17. mongoDB 删除集合后,空间不释放
  18. HDU 1592 Half of and a Half(大数)
  19. EBS获取附件URL
  20. (二)this、call和apply

热门文章

  1. Java自学-初识
  2. html全局属性(收藏)
  3. hashmap的一些基础原理
  4. 2.java设计模式-抽象工厂模式
  5. Java实现类似类似百度搜索模糊关键字
  6. Java基础之java的四大特性
  7. 最短路问题(dijkstral 算法)(优化待续)
  8. Java内存区域与虚拟机类加载机制
  9. Zookeeper + Guava loading cache 实现分布式缓存
  10. C#模拟HTTP POST 请求