Springboot Combat (10) building a RESTful Web service using Spring Boot Actuator

Source: Internet
Author: User
Tags spring boot actuator

First, import dependency

<?XML version= "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.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>

Ii. Building Entities

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

III. Preparation of Controller

 PackageHello;ImportJava.util.concurrent.atomic.AtomicLong;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;Importorg.springframework.web.bind.annotation.ResponseBody; @Controller Public classHelloworldcontroller {Private Static FinalString template = "Hello,%s!"; Private FinalAtomiclong counter =NewAtomiclong (); @GetMapping ("/hello-world") @ResponseBody PublicGreeting SayHello (@RequestParam (name= "name", required=false, defaultvalue= "Stranger") (String name) {return NewGreeting (Counter.incrementandget (), String.Format (template, name)); }}

Iv. Writing configuration Files

server.port:9000management.server.port:9001management.server.address:127.0.0.1

V. Writing the Startup class

 PackageHello;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;/*** Build restful Web services using Spring Boot actuator * */@SpringBootApplication Public classhelloworldapplication { Public Static voidMain (string[] args) {Springapplication.run (helloworldapplication.class, args); }}

Six, write test class

/** Copyright 2012-2014 the original author or authors. * Licensed under the Apache License, Version 2.0 (the "Licens E "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' 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. */ PackageHello;ImportJava.util.Map;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.boot.test.context.SpringBootTest;Importorg.springframework.boot.test.web.client.TestRestTemplate;ImportOrg.springframework.http.HttpStatus;Importorg.springframework.http.ResponseEntity;ImportOrg.springframework.test.context.TestPropertySource;ImportOrg.springframework.test.context.junit4.SpringRunner;Import StaticOrg.assertj.core.api.BDDAssertions.then;/*** Basic Integration tests for service Demo application. * *@authorDave Syer*/@RunWith (Springrunner.class) @SpringBootTest (webenvironment=SpringBootTest.WebEnvironment.RANDOM_PORT) @TestPropertySource (Properties= {"Management.port=0"}) Public classhelloworldapplicationtests {@org. Springframework.boot.context.embedded.LocalServerPortPrivate intPort; @Value ("${local.management.port}")    Private intMgt; @AutowiredPrivatetestresttemplate testresttemplate; @Test Public voidShouldreturn200whensendingrequesttocontroller ()throwsException {@SuppressWarnings ("Rawtypes") responseentity<Map> entity = This. Testresttemplate.getforentity ("http://localhost:" + This. Port + "/hello-world", Map.class);    Then (Entity.getstatuscode ()). Isequalto (Httpstatus.ok); } @Test Public voidShouldreturn200whensendingrequesttomanagementendpoint ()throwsException {@SuppressWarnings ("Rawtypes") responseentity<Map> entity = This. Testresttemplate.getforentity ("http://localhost:" + This. Mgt + "/actuator/info", Map.class);    Then (Entity.getstatuscode ()). Isequalto (Httpstatus.ok); }}

Springboot Combat (10) building a RESTful Web service using Spring Boot Actuator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.