热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringBoot第一个示例

start.spring.io导出示例,竟然发现妹的跑不起来。参考过逃离沙漠的博客,添加了org.sprin

start.spring.io导出示例,竟然发现妹的跑不起来。

参考过逃离沙漠 的博客,添加了

<dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-webartifactId>
dependency> 在主文件添加了

&#64;RequestMapping("/hello")&#64;ResponseBodyString home() {return "Hello ,spring boot!";} 具体看他的示例就可以&#xff0c;启动正常。

localhost:8080/hello访问正常&#xff0c;那么说明官方示例是正常的。

启动信息如下&#xff0c;就自动停止了&#xff0c;也没报错。

. ____ _ __ _ _/\\ / ___&#39;_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | &#39;_ | &#39;_| | &#39;_ \/ _&#96; | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )&#39; |____| .__|_| |_|_| |_\__, | / / / /&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;|_|&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;|___/&#61;/_/_/_/:: 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.springframework.context.annotation.AnnotationConfigApplicationContext&#64;5c18298f: 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.springframework.context.annotation.AnnotationConfigApplicationContext&#64;5c18298f: 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
但可以看到第一行&#xff1a;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&#xff0c;导进来的项目target目录下是空的&#xff0c;那就按套路来打包吧。

项目右键Run As/Maven Build...  

Base directory为${project_loc:springboot-demo} 其中springboot-demo为项目名称。

Goals为package或者clean package -Dmaven.test.skip&#61;true均可&#xff0c;只是前者会多generated-tesrt-sources和surefire-reports&#xff0c;具体


打包好后&#xff0c;刷新项目&#xff0c;target目录下为打包生成的文件


发现target下还是没有class目录呀&#xff0c;那就姑且不管&#xff0c;直接运行jar包启动项目吧。

java -jar springboot-demo-0.0.1-SNAPSHOT.jar > log.file 2>&1 &
访问localhost:8080&#xff0c;页面一闪而过。在target下生成的log.file可以看到启动信息&#xff0c;如上只是到springboot-demo-0.0.1-SNAPSHOT.jar里去读取文件了。
&#xff08;发现了个有趣的事情是&#xff0c;在eclipse中target下没有class目录&#xff0c;而在工作空间的该项目target下是有class目录的。/无语。&#xff09;

实际还是跟上面一样&#xff0c;启动后自动熄火了。
在写这篇文章前是在cmd下启动&#xff0c;发现eclipse下项目启动了&#xff0c;当时莫名其妙的&#xff0c;我实在cmd下的控制台直接启动的jar包啊。

最后发现pom.xml中加上

org.springframework.bootspring-boot-starter-web可在eclipse运行SpringbootDemoApplication启动项目。http://localhost:8080/报404&#xff0c;加个返回结果就好了。

至此SpringBoot的第一个例子也算是跑起来了。跑步起来的原因肯定是由于&#64;SpringBootApplication注解的特殊性&#xff0c;暂时就不深入了解了。

最终pom.xml如下&#xff1a;


4.0.0com.chensan.cdssspringboot-demo0.0.1-SNAPSHOTjarspringboot-demoDemo project for Spring Bootorg.springframework.bootspring-boot-starter-parent1.5.9.RELEASE UTF-8UTF-81.8org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-maven-plugin
主文件&#xff1a;

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;&#64;Controller
&#64;SpringBootApplication
public class SpringbootDemoApplication {&#64;RequestMapping("/hello")&#64;ResponseBodyString home() {return "Hello ,spring boot!";}public static void main(String[] args) {SpringApplication.run(SpringbootDemoApplication.class, args);}
}

访问&#xff1a;http://localhost:8080/hello 效果如下




推荐阅读
author-avatar
红台门
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有