วิธีสร้าง Spring Boot Rest Service ที่ง่ายที่สุดในการฟังบนพอร์ต 8081
เผยแพร่แล้ว: 2020-08-18ในบทช่วยสอนนี้ เราจะพูดถึงขั้นตอนในการสร้างเว็บแอปพลิเคชัน Spring Boot ที่ง่ายที่สุดซึ่งรับฟังพอร์ต 8081
บทช่วยสอนนี้อ้างอิงจากแอปพลิเคชัน Spring Boot HelloWorld ครั้งที่ 1 ในบทความ IntelliJ IDEA
วิธีสร้างแอปพลิเคชัน Spring Boot HelloWorld ครั้งที่ 1 ใน IntelliJ IDEA ด้วยขั้นตอนง่ายๆ ไม่กี่ขั้นตอน
โปรดปฏิบัติตามบทช่วยสอนด้านบนและแก้ไขขั้นตอนที่ 4 และ 5
มาเริ่มกันเลย:
ขั้นตอนที่ 1, 2 และ 3
โปรดทำตามขั้นตอนที่ 1, 2 และ 3 จากบทแนะนำแอปพลิเคชัน Spring Boot ด้านบน
ขั้นตอนที่ 4
- โปรดเพิ่มการพึ่งพาเว็บ Spring Boot
- เว็บสปริง
- บริการเว็บสปริง
- เจอร์ซีย์
- คลิกถัดไป
ขั้นตอนที่ -5
- เปิดไฟล์: CrunchifywebappApplication.java และใส่โค้ดด้านล่าง
- โปรดตรวจสอบให้แน่ใจว่าได้เปลี่ยนชื่อแพ็คเกจและชื่อไฟล์ หากคุณพบข้อผิดพลาดใดๆ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com . example . crunchify . crunchifywebapp ; import org . springframework . boot . SpringApplication ; import org . springframework . boot . autoconfigure . SpringBootApplication ; import org . springframework . web . bind . annotation . GetMapping ; import org . springframework . web . bind . annotation . RequestParam ; import org . springframework . web . bind . annotation . RestController ; /** * @author Crunchify.com * First Spring Hello World Web Application which listens on port 8081. * Version: 1.0 */ // SpringBootApplication: Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. // This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan. // RestController: A convenience annotation that is itself annotated with @Controller and @ResponseBody. // Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default. @ SpringBootApplication @ RestController public class CrunchifywebappApplication { public static void main ( String [ ] args ) { // setProperty: Sets the system property indicated by the specified key. First, if a security manager exists, its SecurityManager.checkPermission method is called with a PropertyPermission(key, "write") permission. This may result in a SecurityException being thrown. // If no exception is thrown, the specified property is set to the given value. System . setProperty ( "server.port" , "8081" ) ; System . setProperty ( "http.maxConnections" , "10" ) ; // run: Static helper that can be used to run a SpringApplication from the specified source using default settings SpringApplication . run ( CrunchifywebappApplication . class , args ) ; } @ GetMapping ( "/crunchify" ) public String heyCrunchify ( @ RequestParam ( value = "name" , defaultValue = "there" ) String crunchify ) { return String . format ( "Hey %s! Congratulations on creating your first Spring Boot Rest Service... by https://crunchify.com" , crunchify ) ; } } |
ขั้นตอนที่ 6
- สร้างโครงการ
- คลิกขวาที่ไฟล์จาวาและเรียกใช้เป็นแอปพลิเคชัน
คุณควรเห็นเอาต์พุตคอนโซลด้านล่าง
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Java HotSpot ( TM ) 64 - Bit Server VM warning : Options - Xverify : none and - noverify were deprecated in JDK 13 and will likely be removed in a future release . . ____ _ __ _ _ / \ \ / ___ '_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | ' _ | '_| | ' _ \ / _ ` | \ \ \ \ \ \ / ___ ) | | _ ) | | | | | | | ( _ | | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.3.RELEASE) 2020-08-17 13:07:33.379 INFO 71788 --- [ main] c.e.c.c.CrunchifywebappApplication : Starting CrunchifywebappApplication on LM-AUN-11022307 with PID 71788 (/Users/appshah/crunchify/intellij/crunchifywebapp/target/classes started by arpshah in /Users/appshah/crunchify/intellij/crunchifywebapp) 2020-08-17 13:07:33.380 INFO 71788 --- [ main] c.e.c.c.CrunchifywebappApplication : No active profile set, falling back to default profiles: default 2020-08-17 13:07:33.771 INFO 71788 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean ' org . springframework . ws . config . annotation . DelegatingWsConfiguration ' of type [org.springframework.ws.config.annotation.DelegatingWsConfiguration$EnhancerBySpringCGLIB$a474bef9] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2020-08-17 13:07:33.814 INFO 71788 --- [ main] .w.s.a.s.AnnotationActionEndpointMapping : Supporting [WS-Addressing August 2004, WS-Addressing 1.0] 2020-08-17 13:07:33.949 INFO 71788 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http) 2020-08-17 13:07:33.954 INFO 71788 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-08-17 13:07:33.954 INFO 71788 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37] 2020-08-17 13:07:34.015 INFO 71788 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-08-17 13:07:34.016 INFO 71788 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 606 ms 2020-08-17 13:07:34.117 INFO 71788 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ' applicationTaskExecutor ' 2020-08-17 13:07:34.202 INFO 71788 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ' ' 2020 - 08 - 17 13 : 07 : 34.210 INFO 71788 --- [ main ] c . e . c . c . CrunchifywebappApplication : Started CrunchifywebappApplication in 1.018 seconds ( JVM running for 1.418 ) |

นี่คือไฟล์ pom.xml ของฉันที่มีการขึ้นต่อกันของเว็บทั้งหมด:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<? 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < parent > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter - parent < / artifactId > < version > 2.3.3.RELEASE < / version > < relativePath / > < ! -- lookup parent from repository -- > < / parent > < groupId > com . example . crunchify < / groupId > < artifactId > crunchifywebapp < / artifactId > < version > 0.0.1 - SNAPSHOT < / version > < name > crunchifywebapp < / name > < description > Crunchify ' s Hello World Web project for Spring Boot < / description > < properties > < java . version > 11 < / java . version > < / properties > < dependencies > < dependency > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter - jersey < / artifactId > < / dependency > < dependency > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter - web < / artifactId > < / dependency > < dependency > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter - web - services < / artifactId > < / dependency > < dependency > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter - test < / artifactId > < scope > test < / scope > < exclusions > < exclusion > < groupId > org . junit . vintage < / groupId > < artifactId > junit - vintage - engine < / artifactId > < / exclusion > < / exclusions > < / dependency > < / dependencies > < build > < plugins > < plugin > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - maven - plugin < / artifactId > < / plugin > < / plugins > < / build > < / project > |
มาเริ่มการทดสอบกันเลย:
ทดสอบ-1
เยี่ยมชม URL: http://localhost:8081/crunchify
ทดสอบ-2
เยี่ยมชม URL: http://localhost:8081/crunchify?name=Crunchify
ยินดีด้วย. คุณสร้าง Spring Boot Web Application สำเร็จในเวลาที่รวดเร็วที่สุด