SpringBootʵս(Ê®)֮ʹÓÃSpring Boot Actuator¹¹½¨RESTful Web
·¢²¼Ê±¼ä£º2020-12-15 07:12:03 ËùÊôÀ¸Ä¿£ºJava À´Ô´£ºÍøÂçÕûÀí
µ¼¶Á£º? Ò»¡¢µ¼ÈëÒÀÀµ ? 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/
? Ò»¡¢µ¼ÈëÒÀÀµ <?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</> groupId>org.springframeworkartifactId>gs-actuator-serviceversion>0.1.0parent> >org.springframework.boot>spring-boot-starter-parent>1.5.8.RELEASEdependenciesdependency> >spring-boot-starter-web>spring-boot-starter-actuator>spring-boot-starter-testscope>testpropertiesjava.version>1.8java.versionbuildpluginsplugin> >spring-boot-maven-plugin> 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; org.springframework.stereotype.Controller; org.springframework.web.bind.annotation.GetMapping; org.springframework.web.bind.annotation.RequestParam; org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloWorldController { private static final String template = "Hello,%s!"; final AtomicLong counter = new AtomicLong(); @GetMapping("/hello-world") @ResponseBody public Greeting sayHello(@RequestParam(name="name",required=false,defaultValue="Stranger") String name) { return Greeting(counter.incrementAndGet(),String.format(template,name)); } } ? ËÄ¡¢±àдÅäÖÃÎļþ server.port: 9000
management.server.port: 9001
management.server.address: 127.0.0.1
? Îå¡¢±àдÆô¶¯Àà org.springframework.boot.SpringApplication; org.springframework.boot.autoconfigure.SpringBootApplication; /** * ʹÓÃSpring Boot Actuator¹¹½¨RESTful Web·þÎñ * */ @SpringBootApplication HelloWorldApplication { void main(String[] args) { SpringApplication.run(HelloWorldApplication.,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. */ java.util.Map; org.junit.Test; org.junit.runner.RunWith; org.springframework.beans.factory.annotation.Autowired; org.springframework.beans.factory.annotation.Value; org.springframework.boot.test.context.SpringBootTest; org.springframework.boot.test.web.client.TestRestTemplate; org.springframework.http.HttpStatus; org.springframework.http.ResponseEntity; org.springframework.test.context.TestPropertySource; 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.) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @TestPropertySource(properties = {"management.port=0"}) HelloWorldApplicationTests { @org.springframework.boot.context.embedded.LocalServerPort int port; @Value("${local.management.port}") mgt; @Autowired private TestRestTemplate testRestTemplate; @Test void shouldReturn200WhenSendingRequestToController() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity<Map> entity = this.testRestTemplate.getForEntity( "http://localhost:" + this.port + "/hello-world",Map.); then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); } @Test void shouldReturn200WhenSendingRequestToManagementEndpoint() this.mgt + "/actuator/info",1)">); then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); } } ? £¨±à¼£ºÀî´óͬ£© ¡¾ÉùÃ÷¡¿±¾Õ¾ÄÚÈݾùÀ´×ÔÍøÂ磬ÆäÏà¹ØÑÔÂÛ½ö´ú±í×÷Õ߸öÈ˹۵㣬²»´ú±í±¾Õ¾Á¢³¡¡£ÈôÎÞÒâÇÖ·¸µ½ÄúµÄȨÀû£¬Ç뼰ʱÓëÁªÏµÕ¾³¤É¾³ýÏà¹ØÄÚÈÝ! |
Ïà¹ØÄÚÈÝ
- java ¨C UnparseableÏ£À°ÓïÈÕÆÚ ¨C SimpleDateFormat
- java ¨C Lambda±í´ïʽ¶ÔÓÚ¶àºËϵͳÓÐʲôºÃ´¦£¿
- java¸´Ï°--Web²¿·Ö
- Java±à³ÌʵÏÖµÝÔöÅÅÐòÁ´±íµÄºÏ²¢
- java ¨C ÏÔʾHTMLʱJEditorPaneÊÇ·ñÓÐCharsetÎÊÌ⣿
- java ¨C Éú³ÉµÄWeb·þÎñ¿Í»§¶ËÀà,ûÓÐsetter
- java ¨C Apache Spark Lambda±í´ïʽ ¨C ÐòÁл¯ÎÊÌâ
- javaÉè¼ÆÄ£Ê½Ö®³éÏñ¹¤³§Ïê½â
- java ¨C ÎÒÓ¦¸Ã½«Hibernate 2.5Éý¼¶/Ç¨ÒÆµ½Hibernate 3.0»¹
- ÉîÈëÀí½âJavaÀ¬»ø»ØÊÕ»úÖÆ
ÍÆ¼öÎÄÕÂ
Õ¾³¤ÍƼö
- java ¨C ÓÃproguard»ìÏýParcelableÀà
- Java Web Services / JAXB ¨C ³éÏó³¬Àà
- Spring Cloud EureKa Ribbon ·þÎñ×¢²á·¢ÏÖÓëµ÷ÓÃ
- java ¨C ´ÓSpring 3.0ÖÐÓÃSpELÌæ»»JSPÖеÄEL
- java ¨C À©Õ¹logbackÅäÖÃ
- ´ò¿ªÎļþ´íÎóÌ«¶à,java.io.FileNotFoundExceptio
- java ¨C ʹÓÃsecp256r1ÇúÏߺÍSHA256Ëã·¨Éú³ÉECDS
- Java ¨C ¾Þ´óµÄÊý¾Ý¼ìË÷
- Objectתjson×Ö·û´®£¬ObjectתMap£¬MapתObject
- MybaitsµÄÌØµã¼°Óŵã
ÈȵãÔĶÁ