最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】VSCode中搭建Spring Boot的示例程序

程序 crifan 399浏览 0评论
折腾:
【未解决】VSCode中用Java的Spring Boot搭建智能电力系统后端框架
期间,先去VSCode中,想办法把官网的demo示例尝试能运行起来。
spring boot
spring boot vscode
Getting Started · Building an Application with Spring Boot
Using Spring Boot
Spring Boot Reference Documentation
VS Code开发Spring Boot应用 – 知乎
VsCode搭建Java开发环境(Spring Boot项目创建、运行、调试) – ice.ko – 博客园
Spring Boot in Visual Studio Code – DEV Community 👩‍💻👨‍💻
算了,还是用VSCode每次打开java文件时的启动页面中,去创建
sprint boot
点击 Create Sprint Boot project
Spring Initializr extension is recommended to help create Spring Boot projects and manage dependencies.
Successfully installed vscjava.vscode-spring-initializr.
就是别人提到的,这个自动帮忙创建sprint boot项目的插件:
vscjava.vscode-spring-initializr
选java的
输入:
com.crifan.xxx
xxx
还是去网上搜搜:
什么是 project artifact id
spring boot project artifact id
Getting Started
<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
Spring Boot Maven Plugin – Usage
Spring Initializr Reference Guide
”3.1. Advanced options
Below the Artifact form field, you’ll find an “Options” link. If you click on that, you’ll see all the available options. Let’s browse through them quickly:
* Group: project coordinates (id of the project’s group, as referred by the groupId attribute in Apache Maven). Also infers the root package name to use.
* Artifact: project coordinates (id of the artifact, as referred by the artifactId attribute in Apache Maven). Also infers the name of the project
* Name: display name of the project that also determines the name of your Spring Boot application. For instance, if the name of your project is my-app, the generated project will have a MyAppApplication class“
artifact,算是project的name
2.2.2的maven版本:
此处感觉算是:
Spring Web
再去选上:
Spring Boot DevTools
Enter继续
选择好保存路径
Successfully generated. Location: /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx
点击Open,打开独立VSCode窗口:
➜  xxx git:(master) ✗ pwd
/Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx
➜  xxx git:(master) ✗ ll
total 56
-rw-r--r--  1 crifan  staff   902B  1 15 22:40 HELP.md
-rwxr-xr-x  1 crifan  staff   9.8K  1 15 22:40 mvnw
-rw-r--r--  1 crifan  staff   6.5K  1 15 22:40 mvnw.cmd
-rw-r--r--  1 crifan  staff   1.6K  1 15 22:40 pom.xml
drwxr-xr-x  4 crifan  staff   128B  1 15 22:40 src
➜  xxx git:(master) ✗ tree .
.
├── HELP.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── crifan
    │   │           └── xxx
    │   │               └── xxx
    │   │                   └── DemoApplication.java
    │   └── resources
    │       ├── application.properties
    │       ├── static
    │       └── templates
    └── test
        └── java
            └── com
                └── crifan
                    └── xxx
                        └── xxx
                            └── DemoApplicationTests.java


16 directories, 7 files
默认生成这些文件。
然后接着就是去看看,怎么调试运行起来spring boot。
去参考:
Building a RESTful Web Service
Serving Web Content with Spring MVC
Building REST services with Spring
Getting Started · Building a RESTful Web Service
Getting Started · Serving Web Content with Spring MVC
去pom.xml中加个json库
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
不过此处故意,删除了xxx
把冗长的路径精简了些
不知道后续是否会出错,先这么弄吧。
或者删除项目,重新建一个?
spring boot restful
RestController (Spring Framework 5.2.3.RELEASE API)
Tutorial · Building REST services with Spring
Spring Boot实战:Restful API的构建 – liuxiaopeng – 博客园
【已解决】Spring Boot运行mvnw spring-boot:run没输出
这下至少有输出了:
✗ mvn spring-boot:run
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.crifan:xxx >-------------------
[INFO] Building xxx 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from central: 
https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/3.1.0/maven-resources-plugin-3.1.0.pom
Downloaded from central: 
https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/3.1.0/maven-resources-plugin-3.1.0.pom
 (7.2 kB at 1.8 kB/s)
...
Downloaded from central: 
https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-engine/5.5.2/junit-jupiter-engine-5.5.2.jar
 (220 kB at 3.4 kB/s)
Downloading from central: 
https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest/2.1/hamcrest-2.1.jar
下载速度有点慢
看来需要先去解决
repo.maven.apache.org下载速度慢的问题
maven下载速度慢的问题
不过后来继续折腾,发现速度还不错:
直到最后build成功了:
➜  projects cd xxx/src/server/xxx/xxx 
➜  xxx git:(master) ✗ mvn spring-boot:run                                                  
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.crifan:xxx >-------------------
[INFO] Building xxx 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) > test-compile @ xxx >>>
Downloading from central: https://repo.maven.apache.org/maven2/org/assertj/assertj-core/3.13.2/assertj-core-3.13.2.jar
Downloading from central: https://repo.maven.apache.org/maven2/net/bytebuddy/byte-buddy/1.10.4/byte-buddy-1.10.4.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.2.2.RELEASE/spring-core-5.2.2.RELEASE.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-test/5.2.2.RELEASE/spring-test-5.2.2.RELEASE.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/xmlunit/xmlunit-core/2.6.3/xmlunit-core-2.6.3.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/xmlunit/xmlunit-core/2.6.3/xmlunit-core-2.6.3.jar (168 kB at 73 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/com/jayway/jsonpath/json-path/2.4.0/json-path-2.4.0.jar
Downloaded from central: https://repo.maven.apache.org/maven2/com/jayway/jsonpath/json-path/2.4.0/json-path-2.4.0.jar (223 kB at 38 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/net/minidev/json-smart/2.3/json-smart-2.3.jar
Downloaded from central: https://repo.maven.apache.org/maven2/net/minidev/json-smart/2.3/json-smart-2.3.jar (120 kB at 13 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/net/minidev/accessors-smart/1.2/accessors-smart-1.2.jar
Downloaded from central: https://repo.maven.apache.org/maven2/net/minidev/accessors-smart/1.2/accessors-smart-1.2.jar (30 kB at 3.1 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/ow2/asm/asm/5.0.4/asm-5.0.4.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-test/5.2.2.RELEASE/spring-test-5.2.2.RELEASE.jar (678 kB at 64 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.29/slf4j-api-1.7.29.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/ow2/asm/asm/5.0.4/asm-5.0.4.jar (53 kB at 5.0 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.29/slf4j-api-1.7.29.jar (41 kB at 3.5 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.2.2.RELEASE/spring-core-5.2.2.RELEASE.jar (1.4 MB at 41 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/assertj/assertj-core/3.13.2/assertj-core-3.13.2.jar (4.5 MB at 62 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/net/bytebuddy/byte-buddy/1.10.4/byte-buddy-1.10.4.jar (3.3 MB at 35 kB/s)
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ xxx ---
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-filtering/3.1.1/maven-filtering-3.1.1.pom
Downloaded from central: 
...
https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/3.0.0/maven-shared-utils-3.0.0.jar (155 kB at 29 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.1.0/plexus-utils-3.1.0.jar (262 kB at 24 kB/s)
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ xxx ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ xxx ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ xxx ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/target/test-classes
[INFO] 
[INFO] <<< spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) < test-compile @ xxx <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) @ xxx ---
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-loader-tools/2.2.2.RELEASE/spring-boot-loader-tools-2.2.2.RELEASE.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-loader-tools/2.2.2.RELEASE/spring-boot-loader-tools-2.2.2.RELEASE.pom (2.5 kB at 6.7 kB/s)
...
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/3.0.1/maven-dependency-tree-3.0.1.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/3.7.0/plexus-archiver-3.7.0.jar (192 kB at 5.6 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/vafer/jdependency/2.1.1/jdependency-2.1.1.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/ow2/asm/asm-analysis/7.0/asm-analysis-7.0.jar (33 kB at 967 B/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/ow2/asm/asm-util/7.0-beta/asm-util-7.0-beta.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/ow2/asm/asm-commons/7.0/asm-commons-7.0.jar (80 kB at 2.3 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/3.0.1/maven-dependency-tree-3.0.1.jar (37 kB at 1.0 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/ow2/asm/asm-util/7.0-beta/asm-util-7.0-beta.jar (81 kB at 2.2 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/vafer/jdependency/2.1.1/jdependency-2.1.1.jar (186 kB at 4.6 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.13/commons-codec-1.13.jar (344 kB at 7.6 kB/s)
[INFO] Attaching agents: []

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)


2020-01-18 20:48:05.030  INFO 19665 --- [  restartedMain] com.crifan.xxx.Greeting  : Starting Greeting on licrifandeMacBook-Pro.local with PID 19665 (/Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/target/classes started by crifan in /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx)
2020-01-18 20:48:05.036  INFO 19665 --- [  restartedMain] com.crifan.xxx.Greeting  : No active profile set, falling back to default profiles: default
2020-01-18 20:48:05.118  INFO 19665 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-01-18 20:48:05.118  INFO 19665 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-01-18 20:48:06.070  INFO 19665 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-18 20:48:06.094  INFO 19665 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-18 20:48:06.095  INFO 19665 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-01-18 20:48:06.177  INFO 19665 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-18 20:48:06.178  INFO 19665 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1060 ms
2020-01-18 20:48:06.237  WARN 19665 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'greeting': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'long' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2020-01-18 20:48:06.240  INFO 19665 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-01-18 20:48:06.256  INFO 19665 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 
但是后面报错了:
【已解决】java的spring boot程序报错:Parameter 0 of constructor in required a bean of type long that could not be found
Spring Boot Maven Plugin – spring-boot:start
Spring Boot Maven Plugin – spring-boot:run
【已解决】启动Spring Boot出错:Parameter 0 of constructor in Greeting required a bean of type long that could not be found
注:如果不加ServingWebContentApplication.java则
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) on project xxx: Unable to find a suitable main class, please add a 'mainClass' property -> [Help 1]
即,找不到main class,无法运行。
现在再去运行
mvn spring-boot:run
至少正常运行了:
✗ mvn spring-boot:run
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.crifan:xxx >-------------------
[INFO] Building xxx 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) > test-compile @ xxx >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ xxx ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ xxx ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ xxx ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ xxx ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/target/test-classes
[INFO] 
[INFO] <<< spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) < test-compile @ xxx <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) @ xxx ---
[INFO] Attaching agents: []


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)


2020-01-31 11:29:39.751  INFO 15591 --- [  restartedMain] c.c.s.ServingWebContentApplication       : Starting ServingWebContentApplication on licrifandeMacBook-Pro.local with PID 15591 (/Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/target/classes started by crifan in /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx)
2020-01-31 11:29:39.754  INFO 15591 --- [  restartedMain] c.c.s.ServingWebContentApplication       : No active profile set, falling back to default profiles: default
2020-01-31 11:29:39.810  INFO 15591 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-01-31 11:29:39.810  INFO 15591 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-01-31 11:29:40.790  INFO 15591 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-31 11:29:40.802  INFO 15591 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-31 11:29:40.802  INFO 15591 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-01-31 11:29:40.875  INFO 15591 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-31 11:29:40.876  INFO 15591 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1066 ms
2020-01-31 11:29:41.070  INFO 15591 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-31 11:29:41.230  INFO 15591 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-01-31 11:29:41.304  INFO 15591 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-31 11:29:41.310  INFO 15591 --- [  restartedMain] c.c.s.ServingWebContentApplication       : Started ServingWebContentApplication in 2.184 seconds (JVM running for 2.636)
然后看看能否去测试
访问
http://localhost:8080/
结果报错:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Jan 31 11:34:22 CST 2020
There was an unexpected error (type=Not Found, status=404).
No message available
换去访问
http://localhost:8080/greeting
就可以了:
第二次再去访问,即可新增id为2
加上name,则也可以正常解析
http://localhost:8080/greeting?name=crifan
另外,给首页中加了静态html
src/server/xxx/xxx/src/main/resources/static/index.html
<!DOCTYPE HTML>
<html>
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p>Get your greeting <a href="/greeting">here</a></p>
</body>
</html>
后,再去访问主页,就可以了:
http://localhost:8080/
Getting Started · Building a RESTful Web Service
再去试试maven打包jar,再用java运行jar
➜  xxx git:(master) ./mvnw clean package
和之前一样,又是没输出
换mvn
mvn clean package
期间要下载很多包
 mvn clean package 
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.crifan:xxx >-------------------
[INFO] Building xxx 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.1.0/maven-clean-plugin-3.1.0.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.1.0/maven-clean-plugin-3.1.0.pom (5.2 kB at 4.1 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-surefire-plugin/2.22.2/maven-surefire-plugin-2.22.2.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-surefire-plugin/2.22.2/maven-surefire-plugin-2.22.2.pom (5.0 kB at 6.1 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire/2.22.2/surefire-2.22.2.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire/2.22.2/surefire-2.22.2.pom (26 kB at 34 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.1.2/maven-jar-plugin-3.1.2.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.1.2/maven-jar-plugin-3.1.2.pom (7.3 kB at 21 kB/s)
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ xxx ---
[INFO] Deleting /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/target
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ xxx ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ xxx ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ xxx ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ xxx ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ xxx ---
Downloading from central: 
https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.22.2/maven-surefire-common-2.22.2.pom

...
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)

...
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.676 s - in com.crifan.xxx.GreetingTests
2020-01-31 12:39:59.309  INFO 19104 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ xxx ---
...
[INFO] Building jar: /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/target/xxx-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.2.2.RELEASE:repackage (repackage) @ xxx ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:14 min
[INFO] Finished at: 2020-01-31T12:40:13+08:00
[INFO] ------------------------------------------------------------------------
生成jar了:
去运行
java -jar target/xxx-0.0.1-SNAPSHOT.jar 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)


2020-01-31 12:42:02.965  INFO 19264 --- [           main] c.c.s.ServingWebContentApplication       : Starting ServingWebContentApplication v0.0.1-SNAPSHOT on licrifandeMacBook-Pro.local with PID 19264 (/Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/target/xxx-0.0.1-SNAPSHOT.jar started by crifan in /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx)
2020-01-31 12:42:02.983  INFO 19264 --- [           main] c.c.s.ServingWebContentApplication       : No active profile set, falling back to default profiles: default
2020-01-31 12:42:04.648  INFO 19264 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-31 12:42:04.680  INFO 19264 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-31 12:42:04.681  INFO 19264 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-01-31 12:42:04.828  INFO 19264 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-31 12:42:04.828  INFO 19264 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1729 ms
2020-01-31 12:42:05.286  INFO 19264 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-31 12:42:05.447  INFO 19264 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2020-01-31 12:42:05.633  INFO 19264 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-31 12:42:05.638  INFO 19264 --- [           main] c.c.s.ServingWebContentApplication       : Started ServingWebContentApplication in 3.507 seconds (JVM running for 4.115)
测试输出也是正常的:
说明和之前运行
mvn spring-boot:run
是一样的。
【总结】
最后,此处可以正常工作的配置和代码是
1.创建Spring Boot模板项目:
(1)安装插件
VSCode的Java启动页->Start->Create Sprint Boot project->Install
(2)创建模板项目
VSCode的Java启动页->Start->Create a Sprint Boot Project->选 Java -> 输入项目包名:com.crifan.xxx ->选上 Spring Web和Spring Boot DevTools-》Enter回车继续-》选择项目保存路径
2.添加配置
src/server/xxx/xxx/.project
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>xxx</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
    </natures>
</projectDescription>
src/server/xxx/xxx/pom.xml
<?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.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.crifan</groupId>
    <artifactId>xxx</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>xxx</name>
    <description>Demo project for Spring Boot</description>


    <properties>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>


        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
3.输入代码
src/server/xxx/xxx/src/main/java/com/crifan/xxx/Greeting.java
package com.crifan.xxx;


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


}
src/server/xxx/xxx/src/main/java/com/crifan/xxx/GreetingController.java
package com.crifan.xxx;


import java.util.concurrent.atomic.AtomicLong;


import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


@RestController
public class GreetingController {


    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();


    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}
src/server/xxx/xxx/src/main/java/com/crifan/xxx/ServingWebContentApplication.java
package com.crifan.xxx;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class ServingWebContentApplication {


    public static void main(String[] args) {
        SpringApplication.run(ServingWebContentApplication.class, args);
    }


}
src/server/xxx/xxx/src/main/resources/static/index.html
<!DOCTYPE HTML>
<html>
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p>Get your greeting <a href="/greeting">here</a></p>
</body>
</html>
4.运行
cd src/server/xxx/xxx
直接调试:
mvn spring-boot:run
或:
mvn clean package
生成jar包:
target/xxx-0.0.1-SNAPSHOT.jar
后,再去运行
java -jar target/xxx-0.0.1-SNAPSHOT.jar
5.测试输出
浏览器中访问
【后记】
Spring Boot Reference Documentation
中解释了,不同的项目的配置的模拟
比如我们此处的是
更多内容详见:
Table 1. Spring Boot application starters

转载请注明:在路上 » 【已解决】VSCode中搭建Spring Boot的示例程序

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.114 seconds, using 20.68MB memory