SpringBoot第一个示例
start.spring.io导出示例,竟然发现妹的跑不起来。
参考过逃离沙漠 的博客,添加了
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>在主文件添加了
@RequestMapping("/hello") @ResponseBody String home() { return "Hello ,spring boot!"; }具体看他的示例就可以,启动正常。
localhost:8080/hello访问正常,那么说明官方示例是正常的。
启动信息如下,就自动停止了,也没报错。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.9.RELEASE)
2017-12-23 23:50:04.031 INFO 6292 --- [ main] c.c.c.s.SpringbootDemoApplication : Starting SpringbootDemoApplication on Jesse-PC with PID 6292 (D:\workspace-eclipse\springboot-demo\target\classes started by acer in D:\workspace-eclipse\springboot-demo)
2017-12-23 23:50:04.031 INFO 6292 --- [ main] c.c.c.s.SpringbootDemoApplication : No active profile set, falling back to default profiles: default
2017-12-23 23:50:04.126 INFO 6292 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springaaa@qq.com5c18298f: startup date [Sat Dec 23 23:50:04 CST 2017]; root of context hierarchy
2017-12-23 23:50:04.921 INFO 6292 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-12-23 23:50:04.937 INFO 6292 --- [ main] c.c.c.s.SpringbootDemoApplication : Started SpringbootDemoApplication in 1.28 seconds (JVM running for 1.864)
2017-12-23 23:50:04.937 INFO 6292 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springaaa@qq.com5c18298f: startup date [Sat Dec 23 23:50:04 CST 2017]; root of context hierarchy
2017-12-23 23:50:04.937 INFO 6292 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
但可以看到第一行:Starting SpringbootDemoApplication on Jesse-PC with PID 6292 (D:\workspace-eclipse\springboot-demo\target\classes started by acer in D:\workspace-eclipse\springboot-demo)
狗日的跑到了target/class下去读取SpringbootDemoApplication,导进来的项目target目录下是空的,那就按套路来打包吧。
项目右键Run As/Maven Build...
Base directory为${project_loc:springboot-demo} 其中springboot-demo为项目名称。
Goals为package或者clean package -Dmaven.test.skip=true均可,只是前者会多generated-tesrt-sources和surefire-reports,具体
打包好后,刷新项目,target目录下为打包生成的文件
发现target下还是没有class目录呀,那就姑且不管,直接运行jar包启动项目吧。
java -jar springboot-demo-0.0.1-SNAPSHOT.jar > log.file 2>&1 &
访问localhost:8080,页面一闪而过。在target下生成的log.file可以看到启动信息,如上只是到springboot-demo-0.0.1-SNAPSHOT.jar里去读取文件了。
(发现了个有趣的事情是,在eclipse中target下没有class目录,而在工作空间的该项目target下是有class目录的。/无语。)
实际还是跟上面一样,启动后自动熄火了。
在写这篇文章前是在cmd下启动,发现eclipse下项目启动了,当时莫名其妙的,我实在cmd下的控制台直接启动的jar包啊。
最后发现pom.xml中加上
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
可在eclipse运行SpringbootDemoApplication启动项目。http://localhost:8080/报404,加个返回结果就好了。至此SpringBoot的第一个例子也算是跑起来了。跑步起来的原因肯定是由于@SpringBootApplication注解的特殊性,暂时就不深入了解了。
最终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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.chensan.cdss</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
主文件:package com.chensan.cdss.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@SpringBootApplication
public class SpringbootDemoApplication {
@RequestMapping("/hello")
@ResponseBody
String home() {
return "Hello ,spring boot!";
}
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
访问:http://localhost:8080/hello 效果如下